DEV Community

Cover image for typeof python
bluepaperbirds
bluepaperbirds

Posted on

5 3

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:

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay