-1

In the following code ;

>>> class fooo():
        def __init__(self):
            self.a=[]
>>> fooo().a
    []
>>> fooo.a
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        fooo.a
    AttributeError: class fooo has no attribute 'a'

i'm a little confuse about the notations fooo().a and fooo.a for accessing the variable "a".Is it that whenever we use fooo().a a object of fooo() class is thrown and it is equivalent to doing as:

>>> m=fooo()
>>> m.a
[]

; while using the notation fooo.a , we are expecting a to be a class/static variable ? Is i'm right or it is something else?

1
  • 2
    Basically, yes. You shouldn't think of fooo().a as a notation in itself; it's a combination of two separate things. As in your second example fooo() on its own creates an instance. Then .a accesses its attribute.
    – BrenBarn
    Sep 28, 2013 at 19:04

1 Answer 1

0

a is an instance attribute. It can be accessed using fooo().a. But note that doing that just throws away the fooo instance created; a more sensible example is:

y = fooo()
y.a = 5

It doesn't exist until an instance of the class is created. Yeah you are totally correct.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.