#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    36
    Rep Power
    11

    function name from a variable


    my problem is that,

    no = 256
    txt = "test"

    def test_256():
    print "this function name is test_256"


    fname = "%s_%s"% (txt, no)

    fname()

    but it doesn't work.
    how can I use a variable to function name


    best regards
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    How about this?
    Code:
    f1 = "hel"
    f2 = "lo"
    def hello(name): 
        print "Hello", name
    
    my_func = eval("%s%s"%(f1, f2))
    
    my_func("World")
    grim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    36
    Rep Power
    11

    Thumbs up thanks


    yes, that worked.

    many thanks

IMN logo majestic logo threadwatch logo seochat tools logo