Testing the data type of a value
Kushal Kumaran
kushal at locationd.net
Sun May 12 11:08:01 EDT 2019
Luuk <luuk at invalid.lan> writes:
> On 12-5-2019 16:07, Piet van Oostrum wrote:
>> Luuk <luuk at invalid.lan> writes:
>>
>>> After thinking about this, (i am prettry new to python), i was doing this:
>>>
>>>>>> print(type(5),type(int),type(5)==type(int),type(5)==int)
>>> <class 'int'> <class 'type'> False True
>>>
>>> Can someone explain why type(5)==int evaluates to True ?
>>>
>>>>> print(int)
>> <class 'int'>
>>
>> The value of int is the class int, which is the class of 5, so type(5) is also that same class int.
>
>
> Maybe i should have asked this:
>
> What is the difference between 'type(5)==int' and 'isinstance(5,int)'
>
> and, if there is no difference why did someone invent 'isinstance()' ...
>
You'll get different behaviour when subclassing is involved.
https://docs.python.org/3/library/functions.html#isinstance
isinstance(object, classinfo)
Return true if the object argument is an instance of the classinfo
argument, or of a (direct, indirect or virtual) subclass thereof.
--
regards,
kushal
More information about the Python-list
mailing list