None

D

Daniel Schüle

Hi NG



def executer(func, para):
func(para)

def foo():
print "function without parameter"

def bar(a):
print "function with 1 parameter"
print a

#executer(foo, None) #error

executer(bar, 100)

print type(foo)
print type(bar)
#seem to have the same type, though one takes parameter and the other doesnt


does this mean that None *is* a value
some special value to show that variable cant be initialized with more
appreciate value
at this stage?

is there a way to allow parameterless functions as parameter in executer?
 
J

John Roth

Daniel Schüle said:
Hi NG



def executer(func, para):
func(para)

def foo():
print "function without parameter"

def bar(a):
print "function with 1 parameter"
print a

#executer(foo, None) #error

executer(bar, 100)

print type(foo)
print type(bar)
#seem to have the same type, though one takes parameter and the other doesnt


does this mean that None *is* a value
some special value to show that variable cant be initialized with more
appreciate value at this stage?

None is an object like any other object in Python. It's conventional
uses are as a return value from functions that don't return a value,
and as an initial value for optional function parameters. These are
conventional; they are not mandated by the language (except that
the return statement without parameters does return None.)
is there a way to allow parameterless functions as parameter in executer?

I'm not sure what you mean by this question. Functions are first class
objects, so you can, of course, pass them to other functions. There
are numerous examples in the standard library of methods which
take functions as a parameter. Look at, for example, the map(),
reduce() and filter() built-in functions, and also at the .sort() method
of lists.

John Roth
 
P

Peter Otten

Daniel Schüle wrote:

def executer(func, para):
func(para)

def foo():
print "function without parameter"

def bar(a):
print "function with 1 parameter"
print a

#executer(foo, None) #error

executer(bar, 100)
[...]

is there a way to allow parameterless functions as parameter in executer?

You can change executer() to accept and pass and arbitrary number of
arguments:

def executer(func, *args):
func(*args)

executer(bar, 100)
executer(foo)

However, excecuter(foo, None) will still fail. See
http://www.python.org/doc/current/tut/node6.html for the details.

As an aside, do the newsgroup - and yourself - a favour and increase your
block indent to 4 spaces.

Peter
 
L

Lee Harr

def executer(func, para):
func(para)

def foo():
print "function without parameter"

How about:

def foo(arg=None):
if arg is not None:
raise TypeError
print "function without parameter"
def bar(a):
print "function with 1 parameter"
print a

#executer(foo, None) #error

executer(bar, 100)

print type(foo)
print type(bar)
#seem to have the same type, though one takes parameter and the other doesnt


does this mean that None *is* a value
some special value to show that variable cant be initialized with more
appreciate value
at this stage?

is there a way to allow parameterless functions as parameter in executer?


I sometimes run in to this situation when creating callback functions.
What if I want to call the function standalone, and not as a callback?

I just use this trick and set the extra parameters to default to None.
 

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
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top