DEV Community

Cover image for typeof python
bluepaperbirds
bluepaperbirds

Posted on

Python Typeof typeof python

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):

class and objects

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'>
Enter fullscreen mode Exit fullscreen mode

what about a list?

>>> type([])
<class 'list'>
>>> 
Enter fullscreen mode Exit fullscreen mode

What if we create a list, store it in a variable and request the type?

>>> x = [1,2,3,4]
>>> type(x)
<class 'list'>
>>> 
Enter fullscreen mode Exit fullscreen mode

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'>
>>
Enter fullscreen mode Exit fullscreen mode

What about text?

>>> x = "hello world"
>>> type(x)
<class 'str'>
Enter fullscreen mode Exit fullscreen mode

Yes, that's a string.
How about numbers?

>>> x = 3
>>> type(x)
<class 'int'>
Enter fullscreen mode Exit fullscreen mode
>>> x = 3.0
>>> type(x)
<class 'float'>
>>>
Enter fullscreen mode Exit fullscreen mode

So you can get the type for any object.

Related links:

Top comments (0)