By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

hmm, lets call it: generic __init__ problem

P: n/a
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[i].__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:
o = funClass.one()
dir(o) ['__doc__', '__init__', '__module__', '__str__', 'attr1', 'attr2'] o.attr1 ''

and: o = funClass.one( attr1='spam', attr2='chicks')
o.attr2 'chicks'

but: dir(o)

['__doc__', '__init__', '__module__', '__str__', 'attr7', 'attr8',
'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[i].__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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Michele Simionato wrote:
This is probably not what you want:

It's not about importing attributes rather than having only one
definition of __init__ in one "metaclass" wich should behave differently
dependent on the name of the subclass (see my answer to Peter). I
found out I have to read lots of stuff to catch up with concepts of
new-style classes and metaclass design.

The solution using super() like Peter suggested is almost perfect
except the name of the class is still hardcoded inside of __init__.

one(object):
def __init__(self, *args, **kwargs):
super(one, self).__init__()
^^^^
r = self.s.get_obj(ldap.schema.ObjectClass, self.__class__.__name__)
...process r...

implies to write another __init__ for every subclass, since "one" wouldn
't match the subclass's name right? Instead of "one" I'd like to have
something that get's the current class at instantiation time. Is that
possible ?

thanks a lot for all your time and help.
Paul
Jul 18 '05 #2

P: n/a
paul kölle <ko*****@uni-weimar.de> wrote in message news:<c1*************@ID-131134.news.uni-berlin.de>...
The solution using super() like Peter suggested is almost perfect
except the name of the class is still hardcoded inside of __init__.

one(object):
def __init__(self, *args, **kwargs):
super(one, self).__init__()
^^^^
r = self.s.get_obj(ldap.schema.ObjectClass, self.__class__.__name__)
...process r...

implies to write another __init__ for every subclass, since "one" wouldn
't match the subclass's name right? Instead of "one" I'd like to have
something that get's the current class at instantiation time. Is that
possible ?


Yes, but it is quite non-trivial to get it right with the current
language.
I consider it to be a wart of super. See Guido's "autosuper" metaclass
and this post of mine for a solution:

http://groups.google.it/groups?hl=it....lang.python.*

Warning: it is not for the faint of heart ;)

Michele Simionato
Jul 18 '05 #3

P: n/a
paul kölle <ko*****@uni-weimar.de> wrote in message news:<c1*************@ID-131134.news.uni-berlin.de>...
<snip considerations about super>

It came to my mind that you may want to read this post of mine (how
to make __init__ calling super automagically):

http://groups.google.it/groups?hl=it....lang.python.*

It does not solve your problem but it can give you some idea.
HTH,

Michele
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.