Comment déterminer une variable Python paramètre#39;s type

Mots clés : pythontypesunsigned16-bitpython

meilleur 5 Réponses Comment déterminer une variable Python paramètre#39;s type

vote vote

100

>>> i = 123 >>> type(i) <type 'int'> >>> type(i) is int True >>> i = 123.456 >>> type(i) <type 'float'> >>> type(i) is float True 
>>> i = 123 >>> isinstance(i, int) True >>> isinstance(i, (float, str, set, dict)) False 
vote vote

89

>>> v = 10 >>> type(v) <type 'int'> 
>>> v = 100000000000000 >>> type(v) <type 'long'> 
>>> v = -10 >>> type(v) <type 'int'> 
>>> v = 'hi' >>> type(v) <type 'str'> 
>>> v = 3.14159 >>> type(v) <type 'float'> 
vote vote

74

print(type(variable_name)) 
vote vote

62

one = 1 
>>> type(one) <type 'int'> 
>>> type(one).__name__ 'int' 
>>> one.__class__ 
class Foo(object):     def foo(self):         self.__class__ 
class Foo(object):     def foo(self):         type(self) 
>>> import sys  >>> format(sys.maxint, '032b') '01111111111111111111111111111111' >>> format(-sys.maxint - 1, '032b') # minimum value, see docs. '-10000000000000000000000000000000' 
>>> sys.float_info sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308,  min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15,  mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1) 
vote vote

51

print type(variable_name) 
In [9]: var = 123  In [10]: var? Type:       int Base Class: <type 'int'> String Form:    123 Namespace:  Interactive Docstring:     int(x[, base]) -> integer 

Questions similaires