1

Could someone, please, explain in plain English what exactly and why is going on in this little snippet:

def foo():
    print "You are calling a function via a variable"

var='foo'
fun_var = globals()[var]

print fun_var # it says `<function list>`
print type(fun_var) # it says `<type 'function'>`

fun_var() # it actually prints out "You are calling a function via a variable"

I understand that via some magic globals() takes the value of var and creates a function with the same name as the value. I also do understand that this may be quite a nice workaround if we don't know the value of var (e.g., it may be set by a user) and we have some functions, one of which we'd like to call depending on that value (e.g. according to the user's preferences).

However, the whole process still looks to me a bit like miracle :). What exactly globals() does here? Where do these powers are coming from? :)

Thank you.

6
  • The documentation about the globals function may be useful to you. Commented Jul 16, 2014 at 17:29
  • 2
    It's not a miracle, nor is it a good idea. I'd forget you ever learned about globals(), lest you decide to use it for something in the future. Commented Jul 16, 2014 at 17:32
  • @Kevin, I've actually see that one skinny line :). Still don't get the mechanism. Commented Jul 16, 2014 at 17:32
  • @Wooble, Then, again, why? Commented Jul 16, 2014 at 17:33
  • For one, because its use can make code very hard to read and debug. Commented Jul 16, 2014 at 17:34

4 Answers 4

4

Actually...

Here you make a function called 'foo' which prints a string

def foo():
    print "You are calling a function via a variable"

Here you assign the string 'foo' to a variable 'var'

var='foo'

The globals() return a dict of global definitions which you can access trough a key, in this case 'foo' returning to fun_var the address of the foo function

fun_var = globals()[var]

Then you can see foo function attributes

print fun_var # it says `<function list>`
print type(fun_var) # it says `<type 'function'>`

Then you call foo function putting ()

fun_var() # it actually prints out "You are calling a function via a variable"

This is the same as:

def foo():
   print('Hello')

fun_var = foo
fun_var()  # Prints 'Hello'
Sign up to request clarification or add additional context in comments.

Comments

1

It's not really that magical. globals() is a dictionary containing all global variable and function declarations, in the style of { key: value }, where the name of the function or variable is the key, and the actual function or variable value is the value. Whenever you have a dictionary, you can ask for the value of a key with dict["key"]. So when you ask for the value of the thing that has key (read: name) foo, you get back the function named foo, as that's what the name is bound to in globals().

That said, only mess with globals() if you absolutely have to. Messing with Python builtins is a dangerous game.

Comments

1

globals() returns a dictionary of all the objects in your global namespace.

globals()['key'] gives you the object mapped to the key in the dictionary.

You are passing in the key for function foo and the dictionary gives you back a reference to the function, which you are then binding to a variable.

No magic involved.

Comments

0

globals() is a dictionary of all the globally accessible objects. You're accessing your function by passing its name as the key to that dictionary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.