Python type() Method (With Examples)

Submitted by IncludeHelp Last updated : December 2, 2023

What is the type() method in Python?

The type() method returns the type of the given object, it is the simplest way to determine the type of any object. The type() method is a built-in method of the Python standard library.

Syntax of type() Method

Python type() method has two variants: type(object) is used to get the type of an object and type(name, base, dict) is used to create a class dynamically with the given values i.e., arguments. The syntaxes for both of the type() method variants are:

# variant 1
type(object)

# variant 2
type(name, bases, dict)

Parameters of type() Method

Both of the type() method's variants have the following parameters:

  • Variant 1: type(object)
    • object: An object whose type we have to find
  • Variant 2: type(name, bases, dict)
    • name: A string that will become the class name.
    • bases: A tuple that specifies the base classes.
    • dict: A dictionary that will be used to create the class's body.

Examples of Python type() Method

Practice these examples to understand the Python tupe() method.

1. Find the type of different objects/variables

Declare different types of objects/variables, assign some values, and find and print the type of all objects.

# declare different types of variables
var1 = None
var2 = 10
var3 = 20.20
var4 = "Hello world!"
var5 = [10, 20, 30]
var6 = ["Hello", "world"]
var7 = [[10, 20], [30, 40], [50, 60]]
var8 = True
var9 = (1, "AmitShukla", 21)
var10 = {10, 20, 30}
var11 = {1: "Amit", 2: "Abhishek"}

# print variables type
print("Type of var1 is:", type(var1))
print("Type of var2 is:", type(var2))
print("Type of var3 is:", type(var3))
print("Type of var4 is:", type(var4))
print("Type of var5 is:", type(var5))
print("Type of var6 is:", type(var6))
print("Type of var7 is:", type(var7))
print("Type of var8 is:", type(var8))
print("Type of var9 is:", type(var9))
print("Type of var10 is: ", type(var10))
print("Type of var11 is: ", type(var11))

Output:

Type of var1 is: <class 'NoneType'>
Type of var2 is: <class 'int'>
Type of var3 is: <class 'float'>
Type of var4 is: <class 'str'>
Type of var5 is: <class 'list'>
Type of var6 is: <class 'list'>
Type of var7 is: <class 'list'>
Type of var8 is: <class 'bool'>
Type of var9 is: <class 'tuple'>
Type of var10 is:  <class 'set'>
Type of var11 is:  <class 'dict'>

2. Find the type of Python class, object, and class members

Declare class, find and print the properties of these classes

# Class Definition
class Car:
    make = "Honda"
    model = "City"
    year = 2022

# Print type of class
print(type(Car))

# Creating on object of the class
car = Car()

# Print type of class objetc
print(type(car))

# Print type of class members
print(type(car.make))
print(type(car.model))
print(type(car.year))

Output:

<class 'type'>
<class '__main__.Car'>
<class 'str'>
<class 'str'>
<class 'int'>

3. Create a dynamic class using type() method

Create a class dynamically using the type() method and then print its details.

Car = type("CAR", (object,), dict(make="Honda", model="City", year=2022))

# Print type and other details of car class
print(type(Car))
print(vars(Car))

Output:

<class 'type'>
{'make': 'Honda', 'model': 'City', 'year': 2022, '__module__': '__main__', '__dict__': 
<attribute '__dict__' of 'CAR' objects>, 
'__weakref__': <attribute '__weakref__' of 'CAR' objects>, '__doc__': None}

Python Tutorial


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.