Skip to content

Instantly share code, notes, and snippets.

@justinvanwinkle
Created August 23, 2013 19:42
Show Gist options
  • Save justinvanwinkle/6323202 to your computer and use it in GitHub Desktop.
Save justinvanwinkle/6323202 to your computer and use it in GitHub Desktop.
bad_attrs = ('__class__', '__doc__', '__init__', '__new__',
'__getattr__', '__getattribute__', '__subclasshook__',
'__str__', '__repr__', '__hash__', '__setattr__')
good_attrs = ('append',)
def get_ops(o):
ops = [x for x in dir(o) if
(x.startswith('__') and x not in bad_attrs) or
x in good_attrs]
return ops
ops = set(get_ops([]) + get_ops({}) + get_ops(()) + get_ops(1) + get_ops(""))
def caller_maker(op):
def caller(self, *args):
return getattr(self.val, op)(*args)
caller.func_name = op
return caller
def OpDecorator(cls):
for op in ops:
setattr(cls, op, caller_maker(op))
return cls
@OpDecorator
class Faker(object):
def __init__(self):
self.val = None
i1, i2 = Faker(), Faker()
i1.val = 1
i2.val = 3
#assert i1 + i2 == 3
assert i1 + 1 == 2
assert 1 + i1 == 2
i1.val = []
i1.append(1)
assert len(i1) == 1
assert i1[0] == 1
@nnewey
Copy link

nnewey commented Aug 23, 2013

What about the simple statement:

i1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment