Skip to content

Instantly share code, notes, and snippets.

@rubensayshi
Created November 8, 2012 17:36
Show Gist options
  • Save rubensayshi/4040279 to your computer and use it in GitHub Desktop.
Save rubensayshi/4040279 to your computer and use it in GitHub Desktop.
import copy
class MyClass(object):
def __init__(self, x):
self.x = x
def __getattr__(self, name):
if name == 'plus_one':
newself = copy.copy(self)
newself.x += 1
return newself
a = MyClass(1)
print a.x
b = a.plus_one
print b.x, a.x
#
# Traceback (most recent call last):
# File "dev.py", line 16, in <module>
# b = a.plus_one
# File "dev.py", line 9, in __getattr__
# newself = copy.copy(self)
# File "/usr/lib/python2.7/copy.py", line 88, in copy
# rv = reductor(2)
#TypeError: 'NoneType' object is not callable
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment