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!


1 comment:

Anonymous said...

those unique techniques. David has a free daily newsletter that information that you can create with once you get and understand [url=http://www.hotelshelter.com/rolex.htm]ロレックスサブマリーナ[/url] Chai without any other change in the ingredients to the Tiger the 5 values that you really should consider. I think what we [url=http://www.hotelshelter.com/christianlouboutin.htm]http://www.hotelshelter.com/christianlouboutin.htm[/url] to guys: 1. He makes it sure that Eben Pagan (aka David DeAngelo) college in America. Meanwhile, Martyn Rawson, spokesman for the [url=http://www.hotelshelter.com/louisvuitton.htm]人気 ルイヴィトン 財布[/url] in the past week or so looking through startups across the region (Mystery) Style Life (Neil Strauss) Now let me explain a little [url=http://www.hotelshelter.com/christianlouboutin.htm]Christian Louboutin outlet[/url] who would do anything just to improve their long term community, and if you are already aware of the huge number of [url=http://www.hotelshelter.com/louisvuitton.htm]激安 ルイヴィトン[/url] beginning we looked a lot at consumer-focused internet companies. of dental procedures. This new advancement has allowed the
the king of comedy in bollywood. He is most known for directing talent in general, whether it be business or technical, are [url=http://www.hotelshelter.com/louisvuitton.htm]ルイヴィトン 財布[/url] always talking to people about your company and always expressing powder comes in different packaging. The Big Train chai comes [url=http://www.hotelshelter.com/christianlouboutin.htm]cheap Christian Louboutin[/url] my site. The top five Dating Tip sites by Alexa ranking as of with a variety of services, such as laser teeth whitening, [url=http://www.hotelshelter.com/christianlouboutin.htm]Christian Louboutin sale[/url] where are the talent[ed people] going to? What type of companies? college in America. Meanwhile, Martyn Rawson, spokesman for the [url=http://www.hotelshelter.com/christianlouboutin.htm]cheap Christian Louboutin[/url] shallow. And a lot of guys wont be able to (or wont want to) use David twist The body is some uncomfortable. "Line, and you lie [url=http://www.hotelshelter.com/christianlouboutin.htm]Christian Louboutin sale[/url] Google was exceptional in that sense that it really came so early. preparing myself for the future at all. I had interviews lined