Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a Python class with lots of attributes - style

I have a class with a number of attributes. Could someone please clarify the differences between setting attributes like a,b,c below and x,y,z. I understand that if you arguments to come in they obviously must be in the init, however, just for setting a number default variables, which is preferred and what are the pros and cons.

class Foo(object):
    a = 'Hello'
    b = 1
    c = False
    def __init__(self):
        self.x = 'World'
        self.y = 2
        self.z = True
like image 628
jonathanbsyd Avatar asked Sep 13 '12 00:09

jonathanbsyd


2 Answers

Variables a, b, and c are class variables. They're evaluated and set once, when the class is first created. Variables x, y, and z are instance variables, which are evaluated and set whenever an object of that class is instantiated.

In general, you use class variables if the same values are going to be used by every instance of the class, and only needs to be calculated once. You use instance variables for variables that are going to be different for each instance of that class.

You can access class variables through the instance variable syntax self.a, but the object in question is being shared between all instances of the class. This doesn't affect much when using immutable data types such as integers or strings, but when using mutable data types such as lists, appending to self.a would cause all instances to see the newly-appended value.

Some examples from IDLE are probably helpful in understanding this:

>>> class Foo(object):
    a = 'Hello'
    b = []
    def __init__(self):
        self.y = []

>>> instance_1 = Foo()
>>> instance_2 = Foo()
>>> instance_1.a
'Hello'
>>> instance_2.a
'Hello'
>>> instance_1.a = 'Goodbye'
>>> instance_1.a
'Goodbye'
>>> instance_2.a
'Hello'
>>> instance_1.b
[]
>>> instance_2.b.append('12345')
>>> instance_1.b
['12345']
>>> instance_2.y
[]
>>> instance_2.y.append('abcde')
>>> instance_2.y
['abcde']
>>> instance_1.y
[]
like image 53
Joe C. Avatar answered Oct 20 '22 07:10

Joe C.


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.)

like image 37
millimoose Avatar answered Oct 20 '22 05:10

millimoose