Is there a difference between
class Foo(object):
bar = 1
def __init__(self):
... etc.
and
class Foo(object):
def __init__(self):
... etc.
Foo.bar = 1
In both cases bar is a property of the class and it is same for all instances of the class, right?
I’d say that the only difference is that in the second case,
Foo.bardoesn’t exist until theFoo.bar = 1statement is executed while in the first case is already available when the class object is created.That’s probably a small difference without any effect in your code (unless there is some code that requires
Foo.barbefore it’s available in the second case). However, I’d say that the first option is better in terms of readability since you don’t have to scroll down to know the attributes for your class, they’re already there.