[ create a new paste ] login | about

Link: http://codepad.org/3XkPyymL    [ raw code | output | fork ]

Python, pasted on Feb 4:
# coding: utf-8
from __future__ import with_statement
from contextlib import contextmanager

@contextmanager
def extract_vars(obj):
  def f(d):
    print d
    for x in d:
      locals()[x] = d[x]
    print locals()
    yield
    for x in d:
      obj.__setattr__(x, d[x])
    
  d = {}
  for x in dir(obj):
    d[x] = obj.__getattribute__(x)
  return f(d)

class Foo(object):
  def __init__(self):
    self.x = 1
    self.l = [1]
  def foo(self):
    with extract_vars(self):
      print x, l
      x = 3
      l.append(2)
    print x, l

f = Foo()
f.foo()


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{'__hash__': <method-wrapper '__hash__' of Foo object at 0x40366a2c>, '__module__': '__main__', '__reduce_ex__': <built-in method __reduce_ex__ of Foo object at 0x40366a2c>, '__new__': <built-in method __new__ of type object at 0x4012ba80>, '__reduce__': <built-in method __reduce__ of Foo object at 0x40366a2c>, '__str__': <method-wrapper '__str__' of Foo object at 0x40366a2c>, '__getattribute__': <method-wrapper '__getattribute__' of Foo object at 0x40366a2c>, '__class__': <class '__main__.Foo'>, 'l': [1], '__delattr__': <method-wrapper '__delattr__' of Foo object at 0x40366a2c>, '__repr__': <method-wrapper '__repr__' of Foo object at 0x40366a2c>, '__setattr__': <method-wrapper '__setattr__' of Foo object at 0x40366a2c>, '__dict__': {'x': 1, 'l': [1]}, 'x': 1, 'foo': <bound method Foo.foo of <__main__.Foo object at 0x40366a2c>>, '__weakref__': None, '__doc__': None, '__init__': <bound method Foo.__init__ of <__main__.Foo object at 0x40366a2c>>}
{'__module__': '__main__', '__reduce_ex__': <built-in method __reduce_ex__ of Foo object at 0x40366a2c>, 'obj': <__main__.Foo object at 0x40366a2c>, 'd': {'__hash__': <method-wrapper '__hash__' of Foo object at 0x40366a2c>, '__module__': '__main__', '__reduce_ex__': <built-in method __reduce_ex__ of Foo object at 0x40366a2c>, '__new__': <built-in method __new__ of type object at 0x4012ba80>, '__reduce__': <built-in method __reduce__ of Foo object at 0x40366a2c>, '__str__': <method-wrapper '__str__' of Foo object at 0x40366a2c>, '__getattribute__': <method-wrapper '__getattribute__' of Foo object at 0x40366a2c>, '__class__': <class '__main__.Foo'>, 'l': [1], '__delattr__': <method-wrapper '__delattr__' of Foo object at 0x40366a2c>, '__repr__': <method-wrapper '__repr__' of Foo object at 0x40366a2c>, '__setattr__': <method-wrapper '__setattr__' of Foo object at 0x40366a2c>, '__dict__': {'x': 1, 'l': [1]}, 'x': 1, 'foo': <bound method Foo.foo of <__main__.Foo object at 0x40366a2c>>, '__weakref__': None, '__doc__': None, '__init__': <bound method Foo.__init__ of <__main__.Foo object at 0x40366a2c>>}, '__dict__': {'x': 1, 'l': [1]}, '__getattribute__': <method-wrapper '__getattribute__' of Foo object at 0x40366a2c>, '__str__': <method-wrapper '__str__' of Foo object at 0x40366a2c>, '__reduce__': <built-in method __reduce__ of Foo object at 0x40366a2c>, '__class__': <class '__main__.Foo'>, 'l': [1], '__delattr__': <method-wrapper '__delattr__' of Foo object at 0x40366a2c>, '__repr__': <method-wrapper '__repr__' of Foo object at 0x40366a2c>, '__setattr__': <method-wrapper '__setattr__' of Foo object at 0x40366a2c>, '__hash__': <method-wrapper '__hash__' of Foo object at 0x40366a2c>, 'x': '__init__', 'foo': <bound method Foo.foo of <__main__.Foo object at 0x40366a2c>>, '__weakref__': None, '__doc__': None, '__init__': <bound method Foo.__init__ of <__main__.Foo object at 0x40366a2c>>, '__new__': <built-in method __new__ of type object at 0x4012ba80>}
Traceback (most recent call last):
  Line 33, in <module>
    f.foo()
  Line 29, in foo
    l.append(2)
  File "/usr/lib/python2.5/contextlib.py", line 29, in __exit__
    self.gen.throw(type, value, traceback)
  Line 12, in f
    yield
  Line 27, in foo
    print x, l
UnboundLocalError: local variable 'x' referenced before assignment


Create a new paste based on this one


Comments: