Remember
type(value) returns the object’s type. A name can be rebound to an object of a different type.
type(value) returns the object’s type. A name can be rebound to an object of a different type.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)Top-level scope
'3'type(value) returns the object’s type. A name can be rebound to an object of a different type.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)
|
v
DISPLAYED RESULT
int
str
FINAL BINDINGS
value --> object 2 (str)
object 2: '3'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)Top-level scope
'3'The example has finished. Compare the final output with the values and references above.
int str
What do you expect this program to print?
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)type() FunctionThe type() function in Python is used to check the type of an object. It can also be used to create new types dynamically, though the most common use is checking the type of a variable.
type(object)type(name, bases, dict)
x = 10
print(type(x))
<class 'int'>
print(type(3.14)) # float
print(type("hello")) # str
print(type([1, 2, 3])) # list
print(type({ "a": 1 })) # dict
<class 'float'>
<class 'str'>
<class 'list'>
<class 'dict'>
value = "123"
if type(value) == str:
print("It's a string!")
It's a string!
MyClass = type('MyClass', (), {'x': 5})
obj = MyClass()
print(obj.x)
5
In this form, type() is a metaclass constructor. It dynamically creates a class with name MyClass and an attribute x = 5.
'int' or 'str' as strings. Use int, str, etc., directly.isinstance() for safer type checking, especially with inheritance.type() is often used to check inputs in functions or debug issues. Knowing how it works helps in understanding Python's dynamic typing.
type(object) returns the class type of the object.type(name, bases, dict) dynamically creates a new class.isinstance() over type() when checking types in practice.Write a Python program that asks the user for input and prints the type of the entered value after converting it to int, float, or str.
value = input("Enter something: ")
try:
print(type(int(value)))
except:
try:
print(type(float(value)))
except:
print(type(value))
Connect what you noticed in the example with the idea below.
type(value) returns the object’s type. A name can be rebound to an object of a different type.
type() changes the value into a different type.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.