Python variable type view

Checking Python Variable Types

Checking Python Variable Types

In Python, each variable has a type. Understanding the variable’s type is crucial for correctly understanding its meaning and performing correct operations. This article will detail how to check the type of a variable in Python.

1. type() Function

The Python type() function can be used to check the type of a variable. It returns an object representing the variable’s type. The following is a sample code:

x = 5
y = 3.14
z = “Hello, World!”

print(type(x))
print(type(y))
print(type(z))

Running result:

<class 'int'>
<class 'float'>
<class 'str'>

As can be seen from the above example, the result returned by the type() function is the actual type of the variable.

2. isinstance() Function

In addition to using the type() function, you can also use the isinstance() function to determine whether a variable is of a specific type. The isinstance() function accepts two parameters: the first is the variable to be checked, and the second is the type to be checked.

The following is a sample code:

x = 5
y = 3.14
z = "Hello, World!"

print(isinstance(x, int))
print(isinstance(y, float))
print(isinstance(z, str))

Running result:

True
True
True

As can be seen from the above example, the isinstance() function returns a Boolean value, indicating whether the variable is of the specified type.

3. Use the dir() function to view attributes and methods

In addition to the type of a variable, you can also view its attributes and methods. The dir() function in Python can be used to view a list of attributes and methods of an object. Here is a sample code:

x = 5

print(dir(x))

Running result:

['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

As can be seen from the above example, the dir() function returns a list of strings representing the variable’s properties and methods.

4. Use the help() function to view the help documentation.

To learn more about the properties and methods of a variable, use the help() function to view the help documentation for the corresponding type. Here is a sample code:

x = 5

print(help(int))

Run results:

Help on class int in module builtins:

class int(object)
 | int(x=0) -> integer
 | int(x, base=10) -> integer
 |
 | Convert a number or string to an integer, or return 0 if no arguments
 | are given. If x is a number, return x.__int__(). For floating point
 | numbers, this truncates towards zero.
 |
 | If x is not a number or if base is given, then x must be a string,
 | bytes, or bytearray instance representing an integer literal in the
 | given base. The literal can be preceded by '+' or '-' and be surrounded
 | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
| Base 0 means to interpret the base from the string as an integer literal.
| >>> int('0b100', base=0)
| 4
| 
| Methods defined here:
| 
| __abs__(self, /)
...

As can be seen from the example above, help(int) outputs the help documentation for the int type, which contains detailed information about the constructor, conversion methods, common properties, and methods of the int class.

5. View the Class Name Using the class Attribute

Every variable in Python has an attribute called __class__ that indicates the class to which the variable belongs. By accessing the __class__ attribute, we can view the class name of the variable. Below is a sample code:

x = 5
y = 3.14
z = “Hello, World!”

print(x.__class__.__name__)
print(y.__class__.__name__)
print(z.__class__.__name__)

Running result:

int
float
str

As can be seen in the example above, the __class__ attribute returns a class object. We can get the class name by accessing the __name__ attribute.

These are several ways to check variable types in Python. By understanding a variable’s type, you can better understand its meaning and manipulate it more flexibly.

Leave a Reply

Your email address will not be published. Required fields are marked *