Stuck on inheritance

Michele Simionato mis6 at pitt.edu
Wed Nov 5 02:49:12 EST 2003


"Matthew" <matthew at newsgroups.com> wrote in message news:<bo968h$bgp$1 at lust.ihug.co.nz>...
> 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
> ining the subclass. Below is a short code fragment. What's wrong? 

The following works:

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)
        self.name = name
        self.info = info

a_call_me = call_me(print_message)
a_call_me()
func = funct(a_call_me, 'fred', [])
func()

I am not sure what you are trying to do, so I followed  your original
code. Your mistake was in super. There was also a problem in
__call__: do you want it to receive zero arguments or a variable
number of arguments? You must be consistent since __call__ and
func must have the same signature in order your code to work.

Notice that "function" is a built-in, so I changed it to "funct".
Moreover, you should use capital names for classes. Finally,
using info=[] is risky: the standard idiom is

info=None
if info is None: info=[]

The reason is that lists are mutables; if you Google on the
newsgroup you will find hundreds of posts explaining the issue.
HTH,

              Michele Simionato




More information about the Python-list mailing list