mirror of
https://github.com/micropython/micropython.git
synced 2026-01-08 05:00:26 +01:00
py/obj: Add support for __float__ and __complex__ functions.
This commit is contained in:
committed by
Damien George
parent
fa15aed0f7
commit
1e87b56219
42
tests/float/float_dunder.py
Normal file
42
tests/float/float_dunder.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# test __float__ function support
|
||||
|
||||
|
||||
class TestFloat:
|
||||
def __float__(self):
|
||||
return 10.0
|
||||
|
||||
|
||||
class TestStrFloat:
|
||||
def __float__(self):
|
||||
return "a"
|
||||
|
||||
|
||||
class TestNonFloat:
|
||||
def __float__(self):
|
||||
return 6
|
||||
|
||||
|
||||
class Test:
|
||||
pass
|
||||
|
||||
|
||||
print("%.1f" % float(TestFloat()))
|
||||
print("%.1f" % TestFloat())
|
||||
|
||||
|
||||
try:
|
||||
print(float(TestStrFloat()))
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
|
||||
try:
|
||||
print(float(TestNonFloat()))
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
|
||||
try:
|
||||
print(float(Test()))
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
Reference in New Issue
Block a user