diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-03-16 15:16:54 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-03-16 15:18:22 +0200 |
commit | 51bbf6a006901fd3877c6abe3d5d67de74401310 (patch) | |
tree | f3a7aa799d92246334cc8b0f9387d422c2ff9db9 /tests/basics/class_str.py | |
parent | 75488d5639819f31b01f76433b82d6259323230d (diff) | |
download | micropython-51bbf6a006901fd3877c6abe3d5d67de74401310.tar.gz micropython-51bbf6a006901fd3877c6abe3d5d67de74401310.zip |
Implement support for __str__ and __repr__ special methods in classes.
Diffstat (limited to 'tests/basics/class_str.py')
-rw-r--r-- | tests/basics/class_str.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/basics/class_str.py b/tests/basics/class_str.py new file mode 100644 index 0000000000..46dae2b5e6 --- /dev/null +++ b/tests/basics/class_str.py @@ -0,0 +1,44 @@ +class C1: + def __init__(self, value): + self.value = value + + def __str__(self): + return "str<C1 {}>".format(self.value) + +class C2: + def __init__(self, value): + self.value = value + + def __repr__(self): + return "repr<C2 {}>".format(self.value) + +class C3: + def __init__(self, value): + self.value = value + + def __str__(self): + return "str<C3 {}>".format(self.value) + + def __repr__(self): + return "repr<C3 {}>".format(self.value) + +c1 = C1(1) +print(c1) + +c2 = C2(2) +print(c2) + +s11 = str(c1) +print(s11) +# This will use builtin repr(), which uses id(), which is of course different +# between CPython and MicroPython +s12 = repr(c1) +print("C1 object at" in s12) + +s21 = str(c2) +print(s21) +s22 = repr(c2) +print(s22) + +c3 = C3(1) +print(c3) |