473,383 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Stuck on inheritance

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()

Jul 18 '05 #1
4 1380
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()
Jul 18 '05 #2
"Matthew" <ma*****@newsgroups.com> wrote in message news:<bo**********@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
Jul 18 '05 #3
mi**@pitt.edu (Michele Simionato) wrote in
news:22**************************@posting.google.c om:
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".

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #4
Duncan Booth <du****@NOSPAMrcp.co.uk> wrote in message news:<Xn***************************@127.0.0.1>...
mi**@pitt.edu (Michele Simionato) wrote in
news:22**************************@posting.google.c om:
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".


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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
3
BeemerBiker
by: BeemerBiker | last post by:
I want to use the C# example shown here Ok, I have done simple overrides before so I coded up protected override Auto... well, I didnt even get that far as intellisense made it clear that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.