?
=?ISO-8859-1?Q?paul_k=F6lle?=
Hi list,
in the course of writing a small app, I tried to design a class, which
would allow to derive its behaviour solely from its name, so that I
would be able to write one abstract class with the logic and get
different objects/instances by subclassing with appropriate names.
Consider the following:
ATTRS = {'one':['attr1', 'attr2'],
'two':['attr3','attr4'],
'three':['attr5','attr6'],
'four':['attr7','attr8']}
class one:
def __init__(self, *args, **kwargs):
## get allowed attributes...
for attr in ATTRS[self.__class__.__name__]:
self.__dict__[attr] = ''
## unknown attributes are silently ignored...
for item in kwargs.keys():
if self.__dict__.has_key( item ):
self.__dict__[item] = kwargs[item]
else:
pass
## init all parents...
parents = self.__class__.__bases__
if parents:
for i in range(len(parents)):
apply(parents.__name__.__init__,\ (self,)+args, kwargs)
class two(one):
def foo(self):
pass
class three(one):
def bar(self):
pass
class four(two, three):
def wiskey_bar(self):
pass
So running:
'bar', 'foo', 'wiskey_bar']
I expected to have all attrs from all parents initialized from:
parents = self.__class__.__bases__
if parents:
for i in range(len(parents)):
apply(parents.__name__.__init__,\ (self,)+args, kwargs)
in the __init__ method. But apparently I have misunderstood the
self.__class__.__bases__ thingy as it does not do what I want ;(
Apart from the obvious mistake I can't figure out:
1) Is this intelligent at all?
2) Is there a better way to do it?
3) How do you change tabwidth in mozilla mail?
thanks
Paul
in the course of writing a small app, I tried to design a class, which
would allow to derive its behaviour solely from its name, so that I
would be able to write one abstract class with the logic and get
different objects/instances by subclassing with appropriate names.
Consider the following:
ATTRS = {'one':['attr1', 'attr2'],
'two':['attr3','attr4'],
'three':['attr5','attr6'],
'four':['attr7','attr8']}
class one:
def __init__(self, *args, **kwargs):
## get allowed attributes...
for attr in ATTRS[self.__class__.__name__]:
self.__dict__[attr] = ''
## unknown attributes are silently ignored...
for item in kwargs.keys():
if self.__dict__.has_key( item ):
self.__dict__[item] = kwargs[item]
else:
pass
## init all parents...
parents = self.__class__.__bases__
if parents:
for i in range(len(parents)):
apply(parents.__name__.__init__,\ (self,)+args, kwargs)
class two(one):
def foo(self):
pass
class three(one):
def bar(self):
pass
class four(two, three):
def wiskey_bar(self):
pass
So running:
['__doc__', '__init__', '__module__', '__str__', 'attr7', 'attr8',''>>> o = funClass.one()
>>> dir(o) ['__doc__', '__init__', '__module__', '__str__', 'attr1', 'attr2']
>>> o.attr1
and: 'chicks'
but:
'bar', 'foo', 'wiskey_bar']
I expected to have all attrs from all parents initialized from:
parents = self.__class__.__bases__
if parents:
for i in range(len(parents)):
apply(parents.__name__.__init__,\ (self,)+args, kwargs)
in the __init__ method. But apparently I have misunderstood the
self.__class__.__bases__ thingy as it does not do what I want ;(
Apart from the obvious mistake I can't figure out:
1) Is this intelligent at all?
2) Is there a better way to do it?
3) How do you change tabwidth in mozilla mail?
thanks
Paul