473,398 Members | 2,393 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,398 software developers and data experts.

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
Jul 18 '05 #1
5 4472
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
0
by: pbb | last post by:
I have a web page on which I dynamically create controls based on the selection a user makes from a dropdownlist (this ddl is not dynamic). Depending on the user's selection, the controls could be...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
25
by: abhinav | last post by:
Hello guys, I am a novice in python.I have to implement a full fledged mail server ..But i am not able to choose the language.Should i go for C(socket API) or python for this project? What are the...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
1
by: jmartmem | last post by:
Greetings, I have a nagging problem with client-side dynamic dependent list boxes that perhaps someone can help me troubleshoot. I have a form with a series of dynamic dependent list boxes....
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.