Paul Morris
Paul Morris

Reputation: 165

Does calling an attribute of a class instantiate an object of that class in Python?

If I have class Foo...

class Foo(object): 

    bar = True        

Upvotes: 1

Views: 32

Answers (1)

wim
wim

Reputation: 362647

does calling Foo.bar instantiate an object of type Foo that gets immediately garbage collected?

No. You've defined Foo.bar here as a class attribute, not an instance attribute.

would I have to include () in between Foo and bar to create an instance of that type?

Yes, calling Foo() is the correct syntax to create a Foo object.

if I do need the parentheses, what method do they call? __init__()?

First __new__ will be called (perhaps from the superclass object) which actually creates the new object, and then __init__ will be called which initialises the already existing object.

Upvotes: 1

Related Questions