As already mentioned, your example works just fine:
>>> from decimal import Decimal >>> a = 5 >>> b = 5.0 >>> c = Decimal(5) >>> type(a) <class 'int'> >>> type(b) <class 'float'> >>> type(c) <class 'decimal.Decimal'>
Listing b (float) as type (int):
>>> type(a)(b) 5
Divide a (int) as type b (float):
>>> type(b)(a) 5.0
Enter a (int) as c-type (decimal):
>>> type(c)(a) Decimal('5')
Note that the Python duck setting often makes this kind of cast unnecessary, although I can provide some scenarios where this would be useful.
source share