mirror of
https://github.com/micropython/micropython.git
synced 2026-01-07 04:30:24 +01:00
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:
13
tests/basics/tests/class_item.py
Normal file
13
tests/basics/tests/class_item.py
Normal 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
|
||||
15
tests/basics/tests/class_number.py
Normal file
15
tests/basics/tests/class_number.py
Normal 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
|
||||
Reference in New Issue
Block a user