0

I want to print function names using this dictionary-

def pressKey() :
    print "is pressed"  
def userLogin() :
    print "Code to login to <Server Name> with <username>"
act={'Login':userLogin,'press':pressKey}

I tried via this (took from an answer)

for key, value in act.iteritems() :
    print value,'()\t',
    act[key]()

The function is being called (the second line of the loop) but this code gives the output as-

<function pressKey at 0x0000000002F2DC88> ()    is pressed

<function userLogin at 0x00000000039BAEB8> ()   Code to login to <Server Name> with <username>

And i want the output as-

pressKey()    ispressed
userLogin()    Code to login to <Server Name> with <username>

Please help how can i get that !.Thanking you in advance.

Emna Ayadi
  • 2,430
  • 8
  • 37
  • 77
  • Possible duplicate of [How to get a function name as a string in Python?](http://stackoverflow.com/questions/251464/how-to-get-a-function-name-as-a-string-in-python) – zondo Jul 12 '16 at 11:11

2 Answers2

1

If you know that they are all functions you could use the __name__ attribute to get the name of the function:

for key, value in act.iteritems():
    print value.__name__, '()\t', 
    act[key]()

note that you actually don't need key since you could use value() instead of act[key]() which is also slightly more efficient:

for value in act.itervalues() :
    print value.__name__, '()\t',
    value()
skyking
  • 13,817
  • 1
  • 35
  • 57
1

Try this:

def pressKey() :
    print "is pressed"  
def userLogin() :
    print "Code to login to <Server Name> with <username>"
act={'Login':userLogin,'press':pressKey}
for key, value in act.iteritems() :
    print value.__name__,'()\t',
    act[key]()