RE: Data Type in Python

You are viewing a single comment's thread from:

Data Type in Python

in python •  7 years ago 

I'm giving you some extensions.

For booleans: True is an alias of 1 and False of 0. They area interchangeable for logical validations. Of course it will be treated differently sometimes in serialization (e.g. json).

For type checking, you also can check if that's an instance of a class, which is better for classes that has inheritance. I give you an example.

class Animal: pass
class Dog(Animal): pass
mypet = Dog()
print(isinstance(mypet, Animal))
# True
print(isinstance(mypet, Dog))
# True
print(isinstance(mypet, int))
# False

Where "type(mypet) is Animal" will be False and "type(mypet) is Dog" will be True, because type() does not check the inheritance.

Keep doing tutorials. :)

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thanks for the more information on type. I will cover it when I will discuss about the OOP's concept in Python.