-2

I'm very new in Py but I know the syntax function(param1,.. paramN) and I can't understand this:

globals()['z'] = 8

Could not be more 'standar' ?

globals('z') = 8  
# or..
globals(param = 'z') = 8

EDIT: thanks to all for yours answers, I just have a couple of hours playing with Python

In retrospective it's "obvious" globals() returns a instance and I acceding to the index when I write globals('z') but the Dict is returned BY REFERENCE because I can change it just doing:

globals()['z'] = 8

or... just another member suggest:

x = globals(); 
x['z'] = 8

All the "magic" was just a pointer to the index :)

Cœur
  • 37,241
  • 25
  • 195
  • 267
boctulus
  • 404
  • 9
  • 15
  • `globals()` returns a `dict` that is then indexed by the string `'z'`. – alecbz Apr 17 '14 at 02:06
  • It's not some funny `globals`-specific syntax. `'z'` just isn't one of the function's arguments. – user2357112 Apr 17 '14 at 02:08
  • @user2357112: so... where is the 'Principle of Least Astonishment' here ? special sintax ? you in another comment tried to ammend but the sintax is there and it's...strange – boctulus Apr 17 '14 at 02:41
  • @Boctulus: Okay, imagine you have a dict `d = {}` and a function `def get_d(): return d`. Then `get_d()['x'] = 3` is equivalent to `d['x'] = 3`. `globals` works the same way. It's just fetching the global variable dict and setting one of its entries. – user2357112 Apr 17 '14 at 02:56
  • @user2357112 : I catch you but it's a kind of polymorphism not allowed for user-defined-functions. Or can you define a function that accept parameters like globals() ? It's like globals() were implementing a kind of 'Array Interface' not avaiable for anybody else :) – boctulus Apr 17 '14 at 03:00
  • @Boctulus: The function I've just shown you works exactly like `globals()`. You can do `get_d()['x'] = 3` or `print 4 + get_d()['y']`. The `'x'` or `'y'` *are not parameters to `globals()`*. `globals()` just returns a dict. What you do with that dict is up to you. – user2357112 Apr 17 '14 at 03:02
  • Also, note that no function anywhere in Python will let you do `f(x) = 3`. You can't assign to a function call. – user2357112 Apr 17 '14 at 03:02

1 Answers1

1

globals is actually a function that takes no parameters and returns a dict, which maps the names of identifiers to their values.

Trying typing just globals() in a python interpreter and examine the result.

>>> z = 8
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'z': 8, '__doc__': None, '__package__': None}

Notice the 'z': 8 part.

But if you run this code you see actually it takes parameters and change that dict / list / whatever: z = 7 globals()['z'] = 8 print("z=", z) # z= 8

Yup, that's true. The dict returned by globals() is where python goes to get the values of global variables, in some sense. Modifying the dict modifies global values directly.


As an FYI, you generally shouldn't be doing stuff like globals()['z'] = 8. Just do z = 8 if you're in global scope already (ie, not inside of a function/class), and inside a function, do:

def foo():
    global z
    z = 8
alecbz
  • 6,292
  • 4
  • 30
  • 50
  • But if you run this code you see actually it takes parameters and change that dict / list / whatever. So... what's my question! HOW is possible that SINTAX – boctulus Apr 17 '14 at 02:09
  • 1
    @Boctulus: It doesn't take parameters. It's the same as `x = globals(); x['z'] = 8`. – user2357112 Apr 17 '14 at 02:19
  • @Boctulus Updated my answer to try address your comment. You seem to maybe be confusing this syntax (which comes from the fact that `globals` is returning a `dict`) and the behavior (which comes from the fact that the returned `dict` is where python stores global variables). – alecbz Apr 17 '14 at 02:20
  • #user2357112 : you are showing globals() in some-how is passing by reference to x. ???? ok but in the ORGINIAL FORM the sintax still tricky! wired.. – boctulus Apr 17 '14 at 02:38
  • @Boctulus I think you think there's something trickier happening than there actually is. Look up syntax for python `dict`s -- that might make things clearer. – alecbz Apr 17 '14 at 03:06