class – Python Classes ( AttributeError: object has no attribute )

class – Python Classes ( AttributeError: object has no attribute )

Edit:
Your code works fine!
What is the Problem?

I still think it is better to move self4 into the init!

Original
I think the most logical thing would be to define self4 on init:

class className(object):
    def __init__(self, self1=1, self2=2, self3=3):
        self.self1 = self1
        self.self2 = self2
        self.self3 = self3
        self.self4 = None

   #rest of class

If anyone still has this issue: you get this error when your indentation is goofed.To fix the asked question above, you just have to add a space before the last two functions definitions, that is;
class className(object):

def __init__(self, self1=1,self2=2,self3=3):
    self.self1=self1
    self.self2=self2
    self.self3=self3

def evaluate(self, self5):
    
    print className.func1(self) + className.func2(self)
    self.self5=self5
    print className.func1(self)

def func1(self):
    return self.self1 + self.self5

def func2(self):
    self.self4 = self.self1+self.self2+self.self3
    return self.self4

just make sure they all have similar indentation, and you are good to go.

class – Python Classes ( AttributeError: object has no attribute )

You should pass self4 in method.

Leave a Reply

Your email address will not be published. Required fields are marked *