473,387 Members | 1,693 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.

Classes derived from dict and eval

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['val'] = 42

def __getitem__(self, item):
# this doesn't get called from the eval statement
print "*", item
return dict.__getitem__(self, item)

a = Foo()

print a['val']
print eval('val*2+6', a)

The first print statements also prints "* val", but __getitem__ is never
called by the evaluation in the eval statement.

Is this a bug? Does anyone have an idea for a workaround? I'm using
Python 2.3.3.

Thanks

Jeremy
Sep 20 '05 #1
3 2677
Jeremy Sanders wrote:
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['val'] = 42

def __getitem__(self, item):
# this doesn't get called from the eval statement
print "*", item
return dict.__getitem__(self, item)

a = Foo()

print a['val']
print eval('val*2+6', a)

The first print statements also prints "* val", but __getitem__ is never
called by the evaluation in the eval statement.

Is this a bug? Does anyone have an idea for a workaround? I'm using
Python 2.3.3.


In [1]: eval?
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in function eval>
Namespace: Python builtin
Docstring:
eval(source[, globals[, locals]]) -> value

Evaluate the source in the context of globals and locals.
The source may be a string representing a Python expression
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mappping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.

globals needs to be a real dictionary. The implementation uses the C
API, it doesn't use the overridden __getitem__. The locals argument,
apparently can be some other kind of mapping.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 20 '05 #2
Jeremy Sanders wrote:
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['val'] = 42

def __getitem__(self, item):
# this doesn't get called from the eval statement
print "*", item
return dict.__getitem__(self, item)

a = Foo()

print a['val']
print eval('val*2+6', a)

The first print statements also prints "* val", but __getitem__ is never
called by the evaluation in the eval statement.

Is this a bug? Does anyone have an idea for a workaround? I'm using
Python 2.3.3.


Try Python 2.4.1:
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
class Foo(dict): ... def __init__(self):
... self.update(globals())
... self['val'] = 42
... def __getitem__(self, item):
... # this doesn't get called from the eval statement
... print "*", item
... return dict.__getitem__(self, item)
... a = Foo()

print a['val'] * val
42 print eval('val*2+6', a)

* val
90

Kent
Sep 21 '05 #3
On Tue, 20 Sep 2005 13:59:50 -0700, Robert Kern wrote:
globals needs to be a real dictionary. The implementation uses the C
API, it doesn't use the overridden __getitem__. The locals argument,
apparently can be some other kind of mapping.


It seems that on Python 2.3 then neither globals or locals accessing by
eval calls the __getitem__ member of the dicts.

Jeremy

Sep 22 '05 #4

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

Similar topics

10
by: Stefan Seefeld | last post by:
hi there, I'm trying to convert a tuple to a list, and get a 'TypeError: list objects are unhashable'. Can anybody enlighten me as to the possible causes for this ? Where does hashing come...
12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
4
by: Axel Straschil | last post by:
Hello! I was fooling around with creating classes for a module with eval, something like: MyModule.py: class Base: init(self, name): self._name = name
49
by: Christopher J. Bottaro | last post by:
I find myself doing the following very often: class Struct: pass .... blah = Struct() blah.some_field = x blah.other_field = y ....
13
by: Jeremy Sanders | last post by:
Is it possible to implement some sort of "lazy" creation of objects only when the object is used, but behaving in the same way as the object? For instance: class Foo: def __init__(self, val):...
3
by: Achim Dahlhoff | last post by:
Hi. I'm trying to find out the diffrence between normal classes and classes derived from built-in types. (Which is causing me trouble trying to instantiate a class using C API calls) >>>...
6
by: Kamilche | last post by:
I have a code snippet here that prints a dict in an arbitrary order. (Certain keys first, with rest appearing in sorted order). I didn't want to subclass dict, that's error-prone, and overkill for...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.