Stuck on inheritance

Michele Simionato mis6 at pitt.edu
Wed Nov 5 14:04:14 EST 2003


"Matthew" <matthew at newsgroups.com> wrote in message news:<boadkv$6fi$1 at lust.ihug.co.nz>...
> 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 thought you wanted self.args to know about the passed arguments.
For instance, passing name and info to the superclass __init__ you get

func.args == ('fred', [])

If you don't pass name and info, func.args will be the empty tuple.
Maybe this is what you want.  In any case func.name=='fred' and 
func.info==[] are true.

> >
> > 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.

Ah, I see now: "name" and "info" are not to be considered part of args
or "kw", you want to pass them separately. Not sure this is a good
idea. Also, the order in

__init__(self, func, *args, **kw, name='', info = [])

is not right, you should write

__init__(self, func, name='', info = [], *args, **kw)

(optional arguments and keyword arguments - in this order - go to the end).

Anyway, you could consider passing only keyword arguments, it is
simpler and more extensible. You can assign defaults value to
a dictionary using dict.setdefault. 

HTH,

            Michele Simionato




More information about the Python-list mailing list