What Is an Object in Python?
In Python, an object is a value that exists in memory.
This idea is important because everything you work with in Python is an object. That includes:
- numbers
- strings
- lists
- dictionaries
- booleans
- functions
If you understand what an object is, it becomes easier to understand types, methods, and some common errors.
Quick example
A quick way to see that different values in Python are objects with different types:
x = 10
print(type(x))
name = "Sam"
print(type(name))
items = [1, 2, 3]
print(type(items))
Expected output:
<class 'int'>
<class 'str'>
<class 'list'>
This shows that:
10is an object of typeint"Sam"is an object of typestr[1, 2, 3]is an object of typelist
If you want to learn more, see the Python type() function explained.
Simple definition
An object is a value stored in memory.
In Python:
- everything you work with is an object
- objects have a type
- different object types behave differently
Common examples of objects include:
- numbers like
5 - strings like
"hello" - lists like
[1, 2, 3] - dictionaries like
{"a": 1} - functions like
print
What makes something an object
In Python, an object usually has a few basic parts:
- A type
The type tells Python what kind of object it is, such asint,str, orlist. - Data
The object stores some value or information. - Behavior
Some objects have methods you can use.
You can inspect an object with built-in functions such as:
type(value)to see its typeid(value)to see its identity in memorydir(value)to see available attributes and methods
Example:
value = "hello"
print(type(value))
print(id(value))
print(dir(value))
If you want to explore these tools, see:
Common examples of objects
Here are some common Python objects:
print(type(5))
print(type("hello"))
print(type([1, 2, 3]))
print(type({"a": 1}))
print(type(True))
Expected output:
<class 'int'>
<class 'str'>
<class 'list'>
<class 'dict'>
<class 'bool'>
So:
5is anintobject"hello"is astrobject[1, 2, 3]is alistobject{"a": 1}is adictobjectTrueis aboolobject
For a broader overview, see Python data types overview.
Objects and variables
A variable is not the object itself.
A variable is just a name that refers to an object.
Example:
x = 10
y = x
print(x)
print(y)
print(x is y)
Expected output:
10
10
True
In this example:
10is the objectxrefers to that objectyalso refers to that same object
So two variables can point to the same object.
This is one reason beginners sometimes get confused. They may think the variable contains the value in a simple way, but in Python it is more accurate to say the variable refers to an object.
Objects and methods
Some objects have methods that you call with dot notation.
A method is a function connected to an object.
For example, a string object has methods like upper():
text = "hello"
print(text.upper())
Output:
HELLO
A list object has methods like append():
items = [1, 2]
items.append(3)
print(items)
Output:
[1, 2, 3]
This helps explain why different objects behave differently:
- strings have string methods
- lists have list methods
- integers do not have the same methods as strings or lists
To learn more, see what is a method in Python.
Why beginners should care
Understanding objects helps with many basic Python ideas.
It helps you understand:
- why different values have different types
- why
"hello".upper()works - why
[1, 2].append(3)works - why some method calls fail on the wrong type
- why errors like AttributeError: object has no attribute happen
For example, this causes an error:
number = 10
print(number.upper())
Why? Because 10 is an int object, and int objects do not have an upper() method.
What this page does not cover
This page defines the term object only.
It does not fully explain:
- classes
- object-oriented programming
- custom objects you create yourself
Those topics are separate and are better learned step by step.
If you want to continue, see:
Common mistakes
Beginners often run into these misunderstandings:
- Thinking only class instances are objects
In Python, basic values like integers and strings are objects too. - Confusing a variable name with the object it refers to
The variable is just a name. - Assuming all objects have the same methods
Different object types have different methods. - Trying to use a list method on a string or a string method on an int
Methods depend on the type of object.
Useful commands for checking an object:
print(type(value))
print(id(value))
print(dir(value))
help(type(value))
These can help you answer questions like:
- What kind of object is this?
- What methods does it have?
- Am I using the right method for this type?
FAQ
Is everything in Python really an object?
Yes. Common values like integers, strings, lists, functions, and even classes are objects in Python.
Is a variable an object?
No. A variable is a name that points to an object.
Is a class the same as an object?
A class is also an object in Python, but beginners usually learn it as a blueprint used to create other objects.
How do I check what kind of object I have?
Use type(value) to see the object's type.