Passing a value with another type of value

Is it possible to distinguish a number from another number? I tried this, but the type returns a string.

n = 1.34 i = 10 type(i)(n) 
+5
source share
1 answer

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.

+4
source

Source: https://habr.com/ru/post/1259973/


All Articles