Jul-30-2017, 04:09 AM
i have modified the code to show some more information.
the module callers.py:
the module callers.py:
from __future__ import division, print_function from_callers_which_namespace_will_this_assignment_be_put_in = 'from callers which namespace will this assignment be put in ?' def a(): print('this is a') return def b(): print('this is b') return def c(): print('this is c') return def d(): print('this is d') return def x(): print('this is x') from_x_which_namespace_will_this_assignment_be_put_in = 'from x which namespace will this assignment be put in ?'[:-2] print('in x locals keys =',repr(locals().keys())) print('in x globals keys =',repr(globals().keys())) a() b() return def y(): print('this is y') c() d() returnthe script:
from __future__ import division, print_function from callers import x, y which_namespace_will_this_assignment_be_put_in = 'which namespace will this assignment be put in ?'[:-2] print('locals keys =',repr(locals().keys())) print('globals keys =',repr(globals().keys())) x() print('foobar') y()and the new output:
Output:locals keys = ['division', 'which_namespace_will_this_assignment_be_put_in', '__builtins__', '__file__', '__package__', 'x', 'y', '__name__', '__doc__', 'print_function']
globals keys = ['division', 'which_namespace_will_this_assignment_be_put_in', '__builtins__', '__file__', '__package__', 'x', 'y', '__name__', '__doc__', 'print_function']
this is x
in x locals keys = ['from_x_which_namespace_will_this_assignment_be_put_in']
in x globals keys = ['a', 'division', 'c', 'b', 'd', '__builtins__', '__file__', '__package__', 'from_callers_which_namespace_will_this_assignment_be_put_in', 'x', 'y', '__name__', '__doc__', 'print_function']
this is a
this is b
foobar
this is y
this is c
this is d
when the script is running it has one namespace referenced both as local and as global. when i think about it, the script just had code not in a function so there was only one context, anyway. but in function x we see where its own symbols are referenced from. and we can see that division and print_function are visible.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.