Implement framework for class-defined built-in operators.

Now working for class-defined methods: __getitem__, __setitem__,
__add__, __sub__.  Easy to add others.
This commit is contained in:
Damien George
2014-01-18 15:31:13 +00:00
parent 0c4e909e76
commit 1d6fc94c16
3 changed files with 100 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
# test class with __getitem__ and __setitem__ methods
class C:
def __getitem__(self, item):
print('get', item)
return 'item'
def __setitem__(self, item, value):
print('set', item, value)
c = C()
print(c[1])
c[1] = 2

View File

@@ -0,0 +1,15 @@
# test class with __add__ and __sub__ methods
class C:
def __init__(self, value):
self.value = value
def __add__(self, rhs):
print(self.value, '+', rhs)
def __sub__(self, rhs):
print(self.value, '-', rhs)
c = C(0)
c + 1
c - 2