In Python, everything is an object (from object orientated programming). This is very different from other programming languages like C++, where object oriented programming is more of an addition to the functional language.
Python is not a functional language, it is highly object orientated. In Python, every object is derived from a class.
Many programming languages support some type of object orientated programming. For example, you could have some objects that are created from the class car (in C# code):
To get the type of an object, you can use the type() function. This returns the class the object is created from.
The type of object can be requested in the Python shell (sometimes named REPL).
As parameter pass the object, it returns the type of the object.
Examples
The examples below run from the Python interactive shell.
Lets try that:
>>> type({})
<class 'dict'>
what about a list?
>>> type([])
<class 'list'>
>>>
What if we create a list, store it in a variable and request the type?
>>> x = [1,2,3,4]
>>> type(x)
<class 'list'>
>>>
Above shows that works too. If you change x to be a dict, it will tell you that.
>>> x = { 'a':1, 'b':2 }
>>> type(x)
<class 'dict'>
>>
What about text?
>>> x = "hello world"
>>> type(x)
<class 'str'>
Yes, that's a string.
How about numbers?
>>> x = 3
>>> type(x)
<class 'int'>
>>> x = 3.0
>>> type(x)
<class 'float'>
>>>
So you can get the type for any object.
Related links:
Top comments (0)