the global keyword:
Rick Johnson
rantingrickjohnson at gmail.com
Tue Jun 21 20:24:31 EDT 2016
On Tuesday, June 21, 2016 at 6:16:09 PM UTC-5, BartC wrote:
> On 21/06/2016 23:20, Rick Johnson wrote:
> > On Tuesday, June 21, 2016 at 12:15:38 PM UTC-5, Random832 wrote:
>
> > Storing dynamic data to global space is almost always
> > foolish, and I'm a fan of name spaces. But i did not
> > recommend such foolish action, i was merely replying to the
> > assertion that "Python does have real globals". She does,
> > you just have look under the tail!
>
> How would you use this idea to solve the OP's problem?
I never intended to solve the OP's problem, i was just
replying to an unfounded assertion. But since you asked, i
will.
> I tried using your method but it didn't work:
Of course not, the code is absurd. Here is one way (ugly).
============================================================
MODULE A.py
============================================================
"""A long long time ago, in a galaxy far away..."""
import sys
import B
sys.modules['__builtin__'].__dict__['BDFL'] = "GvR"
print sys.modules['__builtin__'].__dict__['BDFL']
B.attack()
print sys.modules['__builtin__'].__dict__['BDFL']
============================================================
MODULE B.py
============================================================
"""Module Episode B: Attack of the Rick!"""
import sys
def attack():
sys.modules['__builtin__'].__dict__['BDFL'] = 'Rick'
============================================================
OUTPUT FROM A.py
============================================================
GvR
Rick
Of course, nobody wants to write that much boiler plate over
and over again. So if you search the archives for this string:
PyModule(G.py): Now Python has REAL globals -- and their
scoped to boot!
(Kindly ignore the misspelling of "they're")
...you'll find a thread i authored, that includes an object
exposing a global namespace named "G". Details of how to
inject the symbol G are included. After you have this module
installed, you can write the aforementioned code in a much
simplier form:
============================================================
MODULE A.py
============================================================
"""A long long time ago, in a galaxy far away..."""
import sys
import B
G.BDFL = "GvR"
print G.BDFL
B.attack()
print G.BDFL
============================================================
MODULE B.py
============================================================
"""Module Episode B: Attack of the Rick!"""
import sys
def attack():
G.BDFL = 'Rick'
Sweet!
More information about the Python-list
mailing list