Stuck on inheritance

Matthew matthew at newsgroups.com
Wed Nov 5 03:57:05 EST 2003


> > Hi,
> >
> > I'm trying to learn python and actually do something usefule with it at
the
> > same time. I'm unclear how you correctly pass arguments to a superclass
when
> > initing the subclass. Below is a short code fragment. What's wrong?

> def print_message():
>     print "I am a message"
>
> class call_me(object):
>     def __init__(self, func, *args, **kw):
>         self.func = func
>         self.args = args
>         self.kw = kw
>     def __call__(self,*args,**kw):
>         print "Executing..."
>         return self.func(*self.args, **self.kw)
>
> class funct(call_me):
>     def __init__(self, func, name='', info = []): # description, authour,
etc
>         super(funct, self).__init__(func,name,info)
Not sure about this. Why do you pass name and info to the superclass? I
thought these attributes were stored in the subclass. If not, why the two
following lines?
>         self.name = name
>         self.info = info
>
>
> I am not sure what you are trying to do.
Well, basically I have a dictionary of lists containing unnamed callable
functions to which I wish to add some further info such as an readable name,
description, authour, etc. So help me here...

Using composition I'd have
> class funct(call_me):
>     def __init__(self, func, name='', info = []): # description, authour,
etc
>         self.func = func
>         self.name = name
>         self.info = info
using
a_call_me = call_me(print_message)
my_func = funct(a_call_me, 'fred', [])

Using inheritance I'd have
> class funct(call_me):
>     def __init__(self, func, *args, **kw, name='', info = []): #
description, authour, etc
>         super(call_me, self).__init__(func,*args, **kw)
>         self.name = name
>         self.info = info
using
my_func = funct(print_message, None, None, 'Fred', [])

Is this right or am I *horribly* wrong?! Thanks for the help. matthew.







More information about the Python-list mailing list