hmm, lets call it: generic __init__ problem

Michele Simionato michele.simionato at poste.it
Thu Feb 19 14:28:03 EST 2004


paul kölle <koelle1 at uni-weimar.de> wrote in message news:<c12l27$1dhtdh$1 at 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&lr=&ie=UTF-8&threadm=95aa1afa.0401150715.61e44550%40posting.google.com&rnum=4&prev=/groups%3Fhl%3Dit%26lr%3D%26ie%3DISO-8859-1%26q%3Dsimionato%2Bsuper%2Bgroup%253Acomp.lang.python.*%26btnG%3DCerca%2Bcon%2BGoogle%26meta%3Dgroup%253Dcomp.lang.python.*

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

        Michele Simionato



More information about the Python-list mailing list