How does python identify data type?

How to determine the variable type in Python?

So if you have a variable, for example:

one = 1

You want to know its type?

There are right ways and wrong ways to do just about everything in Python. Here's the right way:

Use type

>>> type(one)
<type 'int'>

You can use the __name__ attribute to get the name of the object. (This is one of the few special attributes that you need to use the __dunder__ name to get to - there's not even a method for it in the inspect module.)

>>> type(one).__name__
'int'

Don't use __class__

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 absolutely necessary.)

Since type gives us the class of the object, we should avoid getting this directly. :

>>> one.__class__

This is usually the first idea people have when accessing the type of an object in a method - they're already looking for attributes, so type seems weird. For example:

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

Don't. Instead, do type(self):

class Foo(object):
    def foo(self):
        type(self)

Implementation details of ints and floats

How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?

In Python, these specifics are implementation details. So, in general, we don't usually worry about this in Python. However, to sate your curiosity...

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

For example, in a 32 bit Python 2, we can deduce that int is a signed 32 bit integer:

>>> import sys

>>> format(sys.maxint, '032b')
'01111111111111111111111111111111'
>>> format(-sys.maxint - 1, '032b') # minimum value, see docs.
'-10000000000000000000000000000000'

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

We can also get some information about Python's floats, which are usually implemented as a double in C:

>>> sys.float_info
sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, 
min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, 
mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1)

Conclusion

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 of Python. I've 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.

This chapter is from the book

Learning About Python Data Types

When a variable is created by an assignment such as variable=value, Python determines and assigns a data type to the variable. A data type defines how the variable is stored and the rules governing how the data can be manipulated. Python uses the variable’s assigned value to determine its type.

So far, this hour has focused on character strings. When the Python statement coffee_cup='tea' was entered, Python saw the characters in quotation marks and determined the variable coffee_cup to be a string literal data type, or str. Table 4.2 lists a few of the basic data types Python assigns to variables.

Table 4.2 Python Basic Data Types

Data Type

Description

float

Floating-point number

int

Integer

long

Long integer

str

Character string or string literal

You can determine which data type Python has assigned to a variable by using the type function. In Listing 4.22, the variables have been assigned two different data types.

Listing 4.22 Assigned Data Types for Variables

>>> coffee_cup='coffee'
>>> type(coffee_cup)
<class 'str'>
>>> cups_consumed=3
>>> type(cups_consumed)
<class 'int'>
>>>

Python assigned the data type str to the variable coffee_cup because it saw a string of characters between quotation marks. However, for the cups_consumed variable, Python saw a whole number, and thus it assigned the integer data type, int.

Making a small change in the cups_consumed variable assignment statement causes Python to change its data type. In Listing 4.23, the number assigned to cups_consumed is reassigned from 3 to 3.5. This causes Python to reassign the data type to cups_consumed from int to float.

Listing 4.23 Changed Data Types for Variables

>>> cups_consumed=3
>>> type(cups_consumed)
<class 'int'>
>>> cups_consumed=3.5
>>> type(cups_consumed)
<class 'float'>
>>>

You can see that Python does a lot of the “dirty work” for you. This is one of the many reasons Python is so popular.

Does Python automatically detect the data type?

Python will automatically determine which data type it will store data as. You need to be aware of data types in order to convert data from one type to another.

How does the interpreter of Python code knows the type of a certain variable?

So to answer the question: Python never determines the type of a variable (label/name), it only uses it to refer to an object and that object has a type. Show activity on this post. The CPython interpreter checks if x and y have the __mul__ method defined, and tries to "make it work" and return a result.

How do you determine the type of data in a variable?

To check the type of any variable data type, we can use the type() function. It will return the type of the mentioned variable data type. Float data type is used to represent decimal point values.