Classes derived from dict and eval

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeremy Sanders

    #1

    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(glo bals())
    self['val'] = 42

    def __getitem__(sel f, 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
  • Robert Kern

    #2
    Re: Classes derived from dict and eval

    Jeremy Sanders wrote:[color=blue]
    > 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(glo bals())
    > self['val'] = 42
    >
    > def __getitem__(sel f, 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.[/color]

    In [1]: eval?
    Type: builtin_functio n_or_method
    Base Class: <type 'builtin_functi on_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
    rkern@ucsd.edu

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

    Comment

    • Kent Johnson

      #3
      Re: Classes derived from dict and eval

      Jeremy Sanders wrote:[color=blue]
      > 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(glo bals())
      > self['val'] = 42
      >
      > def __getitem__(sel f, 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.[/color]

      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.[color=blue][color=green][color=darkred]
      >>> class Foo(dict):[/color][/color][/color]
      ... def __init__(self):
      ... self.update(glo bals())
      ... self['val'] = 42
      ... def __getitem__(sel f, item):
      ... # this doesn't get called from the eval statement
      ... print "*", item
      ... return dict.__getitem_ _(self, item)
      ...[color=blue][color=green][color=darkred]
      >>> a = Foo()
      >>>
      >>> print a['val'][/color][/color][/color]
      * val
      42[color=blue][color=green][color=darkred]
      >>> print eval('val*2+6', a)[/color][/color][/color]
      * val
      90

      Kent

      Comment

      • Jeremy Sanders

        #4
        Re: Classes derived from dict and eval

        On Tue, 20 Sep 2005 13:59:50 -0700, Robert Kern wrote:
        [color=blue]
        > 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.[/color]

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

        Jeremy

        Comment

        Working...