0
  self.assertFalse(b.__is_manual) AttributeError: 'BaseResource' object has no attribute '_Resources__is_manual'

My test_resources.py is

class Resources(TestCase):
    def test_disable_manual_mode(self):
        self.assertFalse(b.__is_manual)
if __name__=='__main__':
    b = base.BaseResource()
    unittest.main()

And My base.py is

class BaseResource(object):
    def __init__(self, index=0, parent=None, **kwargs):
        self.__is_manual = False
    def disable_manual_mode(self):
        self.__is_manual = False

Both are in same directory I want to import __is_manual in test_resouces.py

How do i do it.

I have tried b.__is_manual but it is giving error(mentioned above)

2 Answers 2

1

According to Python docs

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice. The instantiation of object must be inside the test class.

When naming the attribute to __is_manual, you are defining it as a "protected" attribute, and you can not access it. Simplify your code.

class BaseResource(object):
    def __init__(self, index=0, parent=None, **kwargs):
        self.is_manual = False

    def disable_manual_mode(self):
        self.is_manual = False

Also, the instantiation of object must be inside the test class.

class Resources(TestCase):
    def test_disable_manual_mode(self):
        b = base.BaseResource()
        self.assertFalse(b.is_manual)

if __name__=='__main__':
    unittest.main()
Sign up to request clarification or add additional context in comments.

2 Comments

@AbhishekYadav I have improved the answer.
But if i have multiple methods then i need to do instantiation of object multiple times
0

We can't access __is_manual. Because we can't access a variable starting with __ (double underscore).

Comments

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.