Python dynamic function selection

Discussion in 'Python' started by Marijan Tadin, May 12, 2004.

  1. for rule in rules:
    rule()




    "Eric" <> wrote in message
    news:...
    > 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
    Marijan Tadin, May 12, 2004
    #1
    1. Advertising

  2. Marijan Tadin

    Eric Guest

    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
    Eric, May 12, 2004
    #2
    1. Advertising

  3. Hi

    for rule in rules:
    rule()

    because rule it is a function

    Regards,
    Dragos

    ----- Original Message -----
    From: "Eric" <>
    Newsgroups: comp.lang.python
    To: <>
    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
    >
    Dragos Chirila, May 12, 2004
    #3
  4. 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()
    Tuure Laurinolli, May 12, 2004
    #4
  5. 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
    Shalabh Chaturvedi, May 12, 2004
    #5
  6. On 12 May 2004 01:42:02 -0700, (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
    Daniel 'Dang' Griffith, May 12, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Niederberger
    Replies:
    2
    Views:
    16,486
    Christian Kaufhold
    Jan 7, 2005
  2. Andrew Crowe
    Replies:
    1
    Views:
    4,458
    Andrew Crowe
    Sep 13, 2004
  3. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    266
    curiousity
    Nov 21, 2003
  4. Loebb
    Replies:
    0
    Views:
    198
    Loebb
    Feb 23, 2004
  5. Replies:
    1
    Views:
    206
Loading...

Share This Page