Python dynamic function selection

M

Marijan Tadin

for rule in rules:
rule()




Eric said:
The following example will explain what i want to do:
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:
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
 
E

Eric

The following example will explain what i want to do: 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: 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
 
D

Dragos Chirila

Hi

for rule in rules:
rule()

because rule it is a function

Regards,
Dragos

----- Original Message -----
From: "Eric" <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Wednesday, May 12, 2004 11:42 AM
Subject: Python dynamic function selection
 
T

Tuure Laurinolli

Eric said:
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.
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()
 
S

Shalabh Chaturvedi

Eric said:
The following example will explain what i want to do:


print "true"


rule

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

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
 
D

Daniel 'Dang' Griffith

The following example will explain what i want to do:
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:
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
 

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,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top