AppDividend
Latest Code Tutorials

How To Check Datatype In Python

Python isinstance() is an inbuilt method that is often used for testing any type of object because it takes the subclasses into account. One of the best ways to obtain the type of variable is to use the Python type() built-in method.

Every value in Python has a datatype. Everything is an object in Python programming, and data types are classes and variables are instance (object) of these classes.

How To Check Datatype Of Variable In Python

To check the data type of variable in Python, use type() method. Python type() is an inbuilt method that returns the class type of the argument(object) passed as a parameter. You place the variable inside of a type() function, and Python returns the data type.

The type() function is mostly used for debugging purposes. With one argument, the Python type() function returns the type of an object. Python doesn’t have the same datatypes as C/C++.

You can use a __name__ attribute to get the name of an object. In Python, names that start with the underscores are semantically not the part of the public API. So, it’s a best practice for developers to avoid using them.

Syntax

type(object)

Parameters

The object argument is required, and it can be string, integer, list, tuple, set, dictionary, float, etc.

See the following example.

# app.py

str = 'AppDividend'
print(type(str))

int = 123
print(type(int))

float = 21.19
print(type(float))

negative = -19
print(type(negative))

dictionary = {'blog':'AppDividend'}
print(type(dictionary))

list = [1, 2, 3]
print(type(list))

tuple = (19, 21, 46)
print(type(tuple))

See the following output.

How To Check Type Of Variable In Python

Implementation details of ints and floats

In Python 2, an integer is usually a signed integer equal to the implementation’s word width (limited by the system). It’s generally implemented as the long in C. When the integers get bigger than this, we usually convert them to the Python longs (with unlimited precision, not to be confused with C longs).

For example, in the 32 bit Python 2, we can deduce that int is the signed 32-bit integer.

In Python 3, the old integer goes away, and we use (Python’s) long as int, which has unlimited precision.

Don’t use the __class__, a semantically nonpublic API, to get the type of a variable. Use the built-in type() function instead.

And don’t worry too much about the necessary information about Python. I’ve not had to deal with the issues around this myself regularly.

You probably won’t either, and if you do, then you should know enough not to be looking to this answer for what to do.

I highly recommend that you start using the IPython interactive interpreter when dealing with these kinds of questions.

It lets you type the variable_name? And will return the whole list of information about an object, including the type and the docstring of the type.

See the following code.

In [9]: var = 1921

In [10]: var?
Type:       int
Base Class: <type 'int'>
String Form:    1921
Namespace:  Interactive
Docstring:

Don’t use __class__ to check the data type in Python.

In Python, names that start with underscores are semantically not a part of the public API, and it’s a best practice for users to avoid using them. (Except when it’s compulsory.)

Since type gives us a class of the object, we should avoid getting __class__ directly.

class Foo(object):
    def foo(self):
        print(self.__class__)

f = Foo()
f.foo()

Output

<class '__main__.Foo'>

Now, let’s use type() function syntax, which is much better than this one.

<pre class="lang:python decode:true">class Foo(object):
    def foo(self):
        print(type(self))


f = Foo()
f.foo()
</pre>
&nbsp;

Output

<class '__main__.Foo'>

 Don’t use __class__, a semantically nonpublic API, to get the type of a variable. Use type instead.

And don’t worry too much about the implementation details in Python. I have not had to deal with issues around this myself. You probably won’t either, and if you really do, you should know enough not to be looking to this answer for what to do.

IPython interpreter

Use IPython interactive interpreter when dealing with questions like type checking in Python. It lets you type variable_name? And it will return a complete list of information about the object, including a type and the docstring for the type.

In [9]: var = 11

In [10]: var?
Type:       int
Base Class: <type 'int'>
String Form:    11
Namespace:  Interactive
Docstring:
    int(x[, base]) -> integer

Convert a string or number to an integer, if possible. A floating-point argument will be truncated towards zero (this does not include a string representation of a floating-point number!) When converting the string, use an optional base.

It is the error to supply the base when converting a non-string.

If the argument is outside an integer range, a long object will be returned instead.

Conclusion

Finally, Python type() Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.