Search code examples
pythonclassintegerself

class self acts like integer


I didn't understand why. And It will raise an error 'int' object has no attribute 'v', but I want to access the self.v. When I print only self it will print some numbers. I couldn't understand what was going on. Here is my code.

class Candidate:
    def __init__(self,val,pscore,nscore):
        self.v = val
        self.p = pscore
        self.n = nscore

    def __str__(self):
        return f"{self.v} ({self.p},{self.n})"

    def check_plus_pos(self, guessval):
        count = 0
        b = self.v
        a = str(b)
        guessval = str(guessval)
        for i in range(0,len(a)):
            if a[i] == guessval[i]:
                count += 1
        return count

    def check_neg_pos(self, guessval):
        count = 0
        b = self.v
        a = str(b)

        guessval = str(guessval)
        for i in range(0,len(a)):
            for j in range(0,len(guessval)):
                if a[i] == guessval[j] and a[i] != guessval[i]:
                    count += 1
        return count

    def consistent(self, guess):
        if Candidate.check_plus_pos(self,guess.v) == guess.p and Candidate.check_neg_pos(self,guess.v) == guess.n:
            return True
        else:
            return False

The problem occurs at b == self.v I wanted to assign the self.v value to a variable.


Solution

  • To be honest it's pretty hard to understand what that code/class is supposed to do, imho it needs some serious refactoring.

    My guess is you should:

    • instantiate your class somewhere
    • use the method as an instance method, so invoke it with self and not the class name
    • do NOT pass self explicitly at all
    • do NOT use abbreviations which are not commonly known
    • do NOT use single letter variables a means literally nothing
    • use docstrings for non-trivial functions (or as a rule of thumb to most of functions/methods)
    • use type hints, which will help you catch this kind of errors automatically (if you configure your IDE that is)
    • get rid of the assignment to b at all, it's not reused and doesn't seem do anything. This will do the same a = str(self.v)
        ... # all the class related code above
        def check_neg_pos(self, guessval):
            count = 0
            a = str(self.v)
    
            guessval = str(guessval)
            for i in range(0,len(a)):
                for j in range(0,len(guessval)):
                    if a[i] == guessval[j] and a[i] != guessval[i]:
                        count += 1
            return count
    
        def is_consistent(self, guess: Candidate)->bool:
            return bool(self.check_plus_pos(guess.v) == guess.p and self.check_neg_pos(guess.v) == guess.n)
    # Example usage
    candidate_1 = Candidate(1,2,3)
    candidate_2 = Candidate(4,5,6)
    candidates_consistent = candidate_1.is_consistent(guess=candidate_2)
    print(candidates_consistent)