5

Having trouble understanding the problem in my code, new to classes (generally python too, so sorry if I name things wrong). I receive this error:

I think my code is too long winded to include in here, so I made a simplified version to test the concept below.

The question is, how can I create a new self object "self4"? Which would then be available to other functions within the class. Currently I get this error.

AttributeError: 'className' object has no attribute 'self4'

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

filename tester.py

import tester.py

mst=tester.className()

mst.evaluate()
3
  • 1
    Why don't you call self.func2() in __init__ ? Commented Dec 9, 2015 at 10:04
  • I've added how I call this code. I want to be able to call evaluate for different conditions e.g. self1=4 and self1 = 3. Commented Dec 9, 2015 at 10:18
  • 1
    I just ran the code in your question and it works fine....it prints 7 6. The question is not valid... Commented Dec 9, 2015 at 10:27

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed it does, tried it right now. So there is basically no error and therefore no question?!
4

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.

Comments

1

If someone else ever gets this error and it is not an indentation problem, the error can also occur if you accidentally wrote ; Instead of : for the type annotation:

>>> class Example:
...     def __init__(self, var: str) -> None:
...         self.var; str = var
... 
>>> Example('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
AttributeError: 'Example' object has no attribute 'var'

Comments

0

You should pass self4 in method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.