Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python dynamic function selection

Reply
Thread Tools

Python dynamic function selection

 
 
Marijan Tadin
Guest
Posts: n/a
 
      05-12-2004
for rule in rules:
rule()




"Eric" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> 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


 
Reply With Quote
 
 
 
 
Eric
Guest
Posts: n/a
 
      05-12-2004
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
 
Reply With Quote
 
 
 
 
Dragos Chirila
Guest
Posts: n/a
 
      05-12-2004
Hi

for rule in rules:
rule()

because rule it is a function

Regards,
Dragos

----- Original Message -----
From: "Eric" <(E-Mail Removed)>
Newsgroups: comp.lang.python
To: <(E-Mail Removed)>
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
>



 
Reply With Quote
 
Tuure Laurinolli
Guest
Posts: n/a
 
      05-12-2004
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()
 
Reply With Quote
 
Shalabh Chaturvedi
Guest
Posts: n/a
 
      05-12-2004
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


 
Reply With Quote
 
Daniel 'Dang' Griffith
Guest
Posts: n/a
 
      05-12-2004
On 12 May 2004 01:42:02 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
selection structure nested within another selection structure 4Ankit@gmail.com Javascript 1 12-07-2006 11:47 AM
JTable with row selection, but no cell selection Simon Niederberger Java 2 01-07-2005 04:17 PM
JS comparing innerHTML to text selection (window.getSelection() /document.selection) Andrew Crowe HTML 1 09-13-2004 02:22 PM
How to change a range selection to text selection? Loebb Javascript 0 02-23-2004 02:12 PM
HOWTO autopost the selection list upon selection curiousity ASP .Net Mobile 0 11-21-2003 12:57 AM



Advertisments