473,785 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4489
for rule in rules:
rule()


"Eric" <ek****@yahoo.c om> wrote in message
news:b0******** *************** *@posting.googl e.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.c om>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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.co m (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
34240
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
30
3481
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 I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
134
6135
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 different from C. I'm curious -- what is everyone's favorite trick from a non-python language? And -- why isn't it in Python? Here's my current candidate: So the other day I was looking at the language Lua. In Lua, you make a
1
7588
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 dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
0
1345
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 any combination of textboxes, ddls, popup calendars, etc. The properties of the dynamic controls are stored in a SQL database so that my program can know what type of control to render and what items to put in the ddl etc. My problem is that I...
2
4453
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 */ #include <time.h>
25
5768
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 advantages of one over the other in implementing this server.which language will be easier? What are the performance issues?In what language are mail servers generally written?
852
28731
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 my general education. Mark
1
2966
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. Making a selection from list/box A (Qtr) selects a fiscal quarter, which then refreshes the values in list/box B (Mth), which shows the 3 months in that fiscal quarter, which then refreshes the values in list/box C (MthDate), which returns the date...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.