Created
April 1, 2010 04:54
-
-
Save vkolev/351376 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First method to run a function from string | |
# here we use the dir method to get the available functions in django | |
# and we know that the 7-th result in the array is the function get_version() since | |
# I have tested it, but it can be any other | |
import django | |
test = dir(django) | |
print "\nFirst example is running function from django called %s" % test[7] | |
print "result: %s" % django.__getattribute__(test[7])() | |
# Second method to run a function from string | |
# We define a method and then use it, but it can be again any other string | |
# just there is a need of Exception since we usually we don't know if this | |
# method exists | |
def calc(i,j): | |
return i + j | |
print "\nSecond example of running function giving the string name in eval method" | |
print "result: %s" % eval('calc')(2,4) | |
# Third method to run a function from full string | |
# Here we write the whole call as a string and pass it ot exec | |
# It's not so great, but is flexible witch is cool | |
string_function = 'print "hello world"' | |
print "\nThird example of running a function with the exec method" | |
print "result: " | |
exec string_function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment