The main con / gotcha with using class attributes to provide default values for what you intend to be instance-specific data is that the default values are going to be shared between all instances of the class until the value is changed. E.g.:
class Foo(object):
a = []
foo1 = Foo()
foo2 = Foo()
foo1.a.append(123)
foo1.a # [123]
foo2.a # [123]
However, the following will work as one might expect:
class Bar(object):
a = 123
bar1 = Bar()
bar2 = Bar()
bar1.a = 456
bar2.a # 123
To avoid this gotcha while using this technique, you should only use it to set defaults that are immutable values. (E.g. numbers, strings, tuples…)
The reason why Python behaves this way is that when you access an attribute with:
foo.bar
then bar
is first looked up in the object foo
. If the name in not found in the object (i.e. in foo.__dict__
), then the name is looked up in the type of that object. For example, this mechanism is part of how method lookups work. (If you look at the __dict__
of an object, you'll notice its methods aren't there.)
Other minor issues are that this exposes the defaults through the type object when they're intended to be instance-specific; and that it mixes the definitions of class-specific attributes (like constants) if you have any with the defaults. The corollary of the former is that this will let you redefine the value of the default later on for all objects that haven't changed the value yet by assigning to the class attribute. (This could be useful, or confusing; the same precaution against mutable "global" variables applies.)