Stuck on inheritance

M

Matthew

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? Thanks
alot. matthew.

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):
print "Execing..."
return self.func(*self.args, **self.kw)

class function(call_me):
def __init__(self, name='', info = []): # description, authour, etc
super(call_me, self).__init__()
self.name = name
self.info = info

a_call_me = call_me(print_message)
a_call_me()
func = function(a_call_me, 'fred', [])
# also tried func = function(call_me(print_message), 'fred', [])
func()
 
Z

zsolt

I too am very new to the concept of classes and inheritance in python.
I do not have a grasp on how to use "super",nor do I think it is well
documented. The below code alterations shows how I learned to
subclass.

I may not have a complete grasp of what you were trying to do because
I had to add .func to a_call_me where you instanciated function.
Also,I added args to print_message, to get the the args you are adding
passed through. Anyway, I hope I'm not too off the mark.

def print_message(*args, **kw):
print "I am a message"
print args, kw


class call_me(object):
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __call__(self):
print "Execing..."
return self.func(*self.args, **self.kw)

class function(call_me):
def __init__(self, func, name='', info = []): # description,
authour, etc
call_me.__init__(self, func, name, info)
self.name = name
self.info = info



a_call_me = call_me(print_message)
a_call_me()
func = function(a_call_me.func, 'fred', [])
# also tried func = function(call_me(print_message), 'fred', [])
func()
 
M

Michele Simionato

Matthew said:
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
 
D

Duncan Booth

(e-mail address removed) (Michele Simionato) wrote in
Notice that "function" is a built-in, so I changed it to "funct".

Perhaps you are using an unusual variant of Python? The usual variants
don't have a built-in called "function".
 
M

Michele Simionato

Duncan Booth said:
(e-mail address removed) (Michele Simionato) wrote in


Perhaps you are using an unusual variant of Python? The usual variants
don't have a built-in called "function".

You are perfectly right, of course. I think I was mislead by my
secret hope that "function" will become a built-in in the future,
so that we may derive from it our custom functions. I would feel
that possibility somewhat more elegant than the current approach
(i.e. using callable objects). But it is just me, I also would prefer
an iterator base class over the iterator protocol we have now. It is
more of a little wish than a real necessity, anyway.

Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top