hmm, lets call it: generic __init__ problem

paul kölle koelle1 at uni-weimar.de
Wed Feb 18 06:36:08 EST 2004


Hi peter, thanks for your answer.

Peter Otten wrote:

> In current python:
> 
> for parent in  parents:
>     parent.__name__.__init__(self, *args, **kwargs)
> 
> However, you are calling the __init__() method of a string (parent.__name__)
> which is a noop as strings are immutable (I'm surprised it didn't fail).
> I suppose you intended to call parent.__init__() which would result in
> infinite recursion as you determine the class to operate on by
> self.__class__ which will always be the same.
Exactly, but self.__class__.__bases__ gets a tuple with all parents and 
I can't see why it is impossible to iterate over it. But you are right I 
tried parent.__init__() before and it runs into the max recursion error.

> 
> I think this is what you want:
Thanks, this gives me an idea how it can be done.

<-- snipped working example for recursivly importing attributes from a 
dictionary in the current namespace based on superclasses names -->


> 
>>Apart from the obvious mistake I can't figure out:
>>1) Is this intelligent at all?
> 
> Is there a polite way of saying no?
> Seriously, have a look at the tutorial to get uptodate with the current
> python features, then set yourself a real task and try to solve it in the
> simplest and cleanest manner. 
To be honest, it is a "real world" (if there is any) task. The funClass 
thing was just to isolate the problem. I'm writing an LDAP client and 
the data model is made up of ObjectClasses which in turn have required 
and optional attributes. ObjectClasses can inherit attributes from 
"superior" ObjectClasses and the actual dataset (entry) may consist of 
multiple ObjectClasses, collecting all their attributes.

I thought it would be a good idea to mimic that with classes in the 
client code by naming the classes like the ObjectClasses and get the 
required and allowed attributes of the instance from the server in 
__init__ like:
r = self.schema.get_obj(ldap.schema.ObjectClass,\ self.__class__.__name__)
...process r...

This has to be done for every parent and the whole point was *not* to 
write another __init__ for each subclass. So the question boils down to:

1) Do I have to call Super.__init__ explicitely in: Child(Super)'s 
__init__ method  ?

2) If 1): Since I do not want to write a new __init__ for every possible 
subclass, how do I call Super.__init__ if I do not know Super's name?

I am confident that you will *not* need
> multiple inheritance or attributes with pointless default values.
Sorry for the bad example.

> IMHO silently ignoring anything is strongly against python's philosophy.
I can see no harm ignoring useless keyword arguments, hmm.. OK, it hides 
possible errors...

thanks again
  Paul



More information about the Python-list mailing list