473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Subclassed dict as globals

In Python 2.4 the following works:
class G(dict): .... def __getitem__(self, k):
.... return 'K' + k
.... g = G()
exec 'print x, y, z' in g Kx Ky Kz


....while in Python 2.3 it fails with NameError: name 'x' is not defined.
Is this an "accidental feature", or can I count on this working in
future versions of Python? For that matter, is there a way to do this
(intercept global variable accesses) in Python 2.3?

Cheers,

Evan @ 4-am

Jul 18 '05 #1
1 1122
Evan Simpson <ev**@tokenexchange.com> writes:
In Python 2.4 the following works:
>>> class G(dict): ... def __getitem__(self, k):
... return 'K' + k
... >>> g = G()
>>> exec 'print x, y, z' in g Kx Ky Kz >>> [snip] [Is] there a way to do this (intercept global variable accesses)
in Python 2.3?


One can emulate it in a rather limited way in pre 2.4 releases:
cd = compile("print x, y, z", '<string>', 'exec')
glbs = dict(globals())
for id in cd.co_names: glbs[id] = 'K' + id

exec cd in glbs

Kx Ky Kz

Special names can be used only as constants. It is better suited
for eval() than exec.

Lenard Lindstrom
<le***@telus.net>
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains...
15
by: Irmen de Jong | last post by:
Hi I have this dict that maps a name to a sequence of other names. I want to have it reversed, i.e., map the other names each to the key they belong to (yes, the other names are unique and they...
3
by: Jeremy Sanders | last post by:
Hi - I'm trying to subclass a dict which is used as the globals environment of an eval expression. For instance: class Foo(dict): def __init__(self): self.update(globals()) self = 42
3
by: CJM | last post by:
I'm currently working on an assembly that has both forms and controls in it. When I create a new control based on UserControl and compile, it shows up in the toolbox for all forms in this...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
4
by: bullockbefriending bard | last post by:
Given: class Z(object): various defs, etc. class ZList(list): various defs, etc. i would like to be able to replace
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
5
by: Janto Dreijer | last post by:
It seems eval is modifying the passed in locals/globals. This is behaviour I did not expect and is really messing up my web.py app. Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12) on linux2...
3
by: bruce | last post by:
hi guys/gals... got a basic question that i can't get my hands around. i'm trying to programatically create/use a list/tuple (or whatever the right phrase in pyton is!!) basically,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.