By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

Python dynamic function selection

P: n/a
The following example will explain what i want to do:
def func(): print "true" rules=(func,)
for rule in rules: rule

I expect the final function to print true, but instead i have
<function func at 0x00DC6EB0>

How do i get it to print true. I know if i had parameters in rule
like: def func(var): print var rules=(func,)
for rule in rules:

rule("true")
it will work. But in my case i don't need to pass any parameters.

How do i get the former method to print instead of returning a
function?

Eric
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
for rule in rules:
rule()


"Eric" <ek****@yahoo.com> wrote in message
news:b0************************@posting.google.com ...
The following example will explain what i want to do:
def func(): print "true" rules=(func,)
for rule in rules: rule

I expect the final function to print true, but instead i have
<function func at 0x00DC6EB0>

How do i get it to print true. I know if i had parameters in rule
like: def func(var): print var rules=(func,)
for rule in rules:

rule("true")
it will work. But in my case i don't need to pass any parameters.

How do i get the former method to print instead of returning a
function?

Eric


Jul 18 '05 #2

P: n/a
Hi

for rule in rules:
rule()

because rule it is a function

Regards,
Dragos

----- Original Message -----
From: "Eric" <ek****@yahoo.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Wednesday, May 12, 2004 11:42 AM
Subject: Python dynamic function selection

The following example will explain what i want to do:
def func(): print "true" rules=(func,)
for rule in rules: rule

I expect the final function to print true, but instead i have
<function func at 0x00DC6EB0>

How do i get it to print true. I know if i had parameters in rule
like: def func(var): print var rules=(func,)
for rule in rules:

rule("true")
it will work. But in my case i don't need to pass any parameters.

How do i get the former method to print instead of returning a
function?

Eric
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #3

P: n/a
Eric wrote:
def func(): print "true"
rules=(func,)
for rule in rules: rule

I expect the final function to print true, but instead i have
<function func at 0x00DC6EB0>


Why? You are not calling the function, so in the interactive interpreter
you get the representation instead.
for rule in rules:


rule("true")


Call it without passing arguments. The parentheses denote that the name
to their left is callable and should be called with whatever arguments
there are inside them, there can also be 0 arguments.

for rule in rules:
rule()
Jul 18 '05 #4

P: n/a
Eric wrote:
The following example will explain what i want to do:
def func():
print "true"
rules=(func,)
for rule in rules:

rule

I expect the final function to print true, but instead i have
<function func at 0x00DC6EB0>


<another example snipped>
How do i get the former method to print instead of returning a
function?

Eric


As others have mentioned, you have to use parenthesis () to call
anything, function or otherwise.

The function name by itself referes to the function. This feature allows
functions to be used as first-class objects easily - they can be passed
around like data.

def f():
print 'true'

g = f
g()

HTH,
Shalabh
Jul 18 '05 #5

P: n/a
On 12 May 2004 01:42:02 -0700, ek****@yahoo.com (Eric) wrote:
The following example will explain what i want to do:
def func(): print "true" rules=(func,)
for rule in rules: rule

I expect the final function to print true, but instead i have
<function func at 0x00DC6EB0>

How do i get it to print true. I know if i had parameters in rule
like: def func(var): print var rules=(func,)
for rule in rules:

rule("true")
it will work. But in my case i don't need to pass any parameters.

How do i get the former method to print instead of returning a
function?


In Python, as in C and many other languages (but not BASIC or
Pascal/Delphi and probably others), to call/invoke/execute a function,
you need to put parentheses after the name. This is what the others'
examples have shown:
for rule in rules:
rules() # call the function
If this were not the way it worked, then in the line above it, where
you have:
rules=(func,)
it would have (called and) printed true and returned None
(implicitly), and rules would point to a (None,) tuple.

--dang
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.