diff options
| author | Damien George | 2014-01-18 15:31:13 +0000 |
|---|---|---|
| committer | Damien George | 2014-01-18 15:31:13 +0000 |
| commit | 1d6fc94c166f459cf8a6667661be0985fab7d446 (patch) | |
| tree | 5bcf09c17cffb0f01a7d266e8961ab3d6551b826 /tests/basics | |
| parent | 0c4e909e766b9ab27cd74038f5fe52e308fa81bd (diff) | |
Implement framework for class-defined built-in operators.
Now working for class-defined methods: __getitem__, __setitem__,
__add__, __sub__. Easy to add others.
Diffstat (limited to 'tests/basics')
| -rw-r--r-- | tests/basics/tests/class_item.py | 13 | ||||
| -rw-r--r-- | tests/basics/tests/class_number.py | 15 |
2 files changed, 28 insertions, 0 deletions
diff --git a/tests/basics/tests/class_item.py b/tests/basics/tests/class_item.py new file mode 100644 index 000000000..6061f2607 --- /dev/null +++ b/tests/basics/tests/class_item.py @@ -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 diff --git a/tests/basics/tests/class_number.py b/tests/basics/tests/class_number.py new file mode 100644 index 000000000..e1dbf4a26 --- /dev/null +++ b/tests/basics/tests/class_number.py @@ -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 |
