Wednesday, June 20, 2007

static fields in python

In python language the classes are behaving some times different then in other languages, for example the static fields. If you'll read on the net about static fields in python you'll get many solutions one more complex then the other. The simplest method I've stumble upon when coding is to declare and initialize the attribute in the class not in the constructor like in the example below:

#!/usr/bin/python

class father(object):
_class_atributes = []
_object_atributes = None

def __init__(self, cls_att, obj_att) :
# init the obj att
self._object_atributes = []

self._class_atributes.append(cls_att)
self._object_atributes.append(obj_att)

def __str__(self) :
print 'called by ' + str(self.__class__)
print 'class att: ' + str(self._class_atributes)
print 'object att: ' + str(self._object_atributes)
return ""

f1 = father(1, 1);
str(f1);
f2 = father(2, 2);

str(f1);
str(f2);

and the output is :


[andrei@aculapov scripts]# ./example1.py
called by
class att: [1]
object att: [1]
called by
class att: [1, 2]
object att: [1]
called by
class att: [1, 2]
object att: [2]
[andrei@aculapov scripts]#


As you can see the two instances of the father class share the _class_atributes list. This is because it's created by the class and not at instance creation.

Now comes an inheritance issue. If you make some class or classes that inherits our father class, then _class_atribute will be common to all objects that inherited our base class. So let us add two more classes :


class One(father):

def __init__(self):
super(One, self).__init__(1, 1)

class Two(father):

def __init__(self):
super(Two, self).__init__(2, 2)

one = One();
str(one);
two = Two();

str(one);
str(two);


and the new output it will be:


[root@aculapov scripts]# ./inh.py
called by
class att: [1]
object att: [1]
called by
class att: [1, 2]
object att: [1]
called by
class att: [1, 2]
object att: [2]
[root@aculapov scripts]#



Nice thing I can say!