1

Python is raising the below exception when running my script. Could somebody please explain why this is happening and how to fix it?

Traceback (most recent call last):
  File "C:\Users\vinsunny\AppData\Local\Programs\Python\Python36\test.py", line 28, in <module>
    test.my_func()
  File "C:\Users\vinsunny\AppData\Local\Programs\Python\Python36\test.py", line 23, in my_func
    print('non secret no. from test_func: ', self.number2)
AttributeError: 'Test' object has no attribute 'number2'"
class Test():

    __class_secret_number = 100

    def __secret_func(self):
        self.number1 = 1
        self.__secret_number1 = 11
        print('You cannot see this unless accessing from the Class itself')

    def test_func(self):
        self.number2 = 2
        self.__secret_number2 = 22

    def my_func(self):
        self.__secret_func()
        print('Secret no. from class: ', self.__class_secret_number)

        print('non secret no. from secret func: ', self.number1)
        print('Secret no. from secret func: ', self.__secret_number1)

        # These two prints raise an exception.
        print('non secret no. from test_func: ', self.number2)
        print('Secret no. from test_func: ', self.__secret_number2)

test = Test()
test.my_func()
2
  • Facing this issue. Digged in google, no solution found. Commented Feb 16, 2018 at 16:19
  • 2
    You don't call test_func. So number2 is never assigned. Commented Feb 16, 2018 at 16:20

2 Answers 2

2

At the time the offending print statement is executed, the number2 attribute on your Test instance has not been defined.

The first time the number2 attribute gets set is in the test_func() method. i.e. Attempting to access number2 before calling test_func() will result in the error you are observing.

You should assign a default value to self.number2 in the initializer for the Test class. You could also call test_func() before calling my_func(). But, either way, it's still good practice to initialize all instance attributes in Test.__init__.

The interpreter will raise an exception for the same reason when you try to access the undefined attribute __secret_number_2 in my_func().

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks dude. It's working. Didn't think in that way.
Just created stackoverflow account so i couldn't upvote since i have <15 reputation.
Accepted. Yea sure i will come back once i have 15 reputation.
Thanks! Glad I could help!
came back with 15+ reputation.. Upvoted your answer.. Thanks once again..
0

test_func is never called, so number2 and __secret_number2 doesn't exist.

Need to call the function before try to access them, or init them with init method.

1 Comment

Yeah bro.. Tried it. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.