How to Create an Object in Python
To create an object in Python, you first define a class, then call the class name like a function.
This page shows you how to:
- Create an object from a class
- Understand the difference between the class name and the object variable
- Pass starting values when creating an object
- Use the object’s data and methods right away
Quick answer #
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog("Max")
print(my_dog.name)
Output:
Max
Create a class, call the class name like a function, and store the new object in a variable.
What this page helps you do #
- Create an object from a class
- Understand the class name vs the object variable
- Pass starting values when creating an object
- Use the object’s data and methods
What an object is in simple terms #
A class is a blueprint.
An object is one real thing created from that blueprint.
For example, if Dog is a class, then Dog("Max") creates one dog object.
Important ideas:
- A class is a blueprint
- An object is one real item made from that blueprint
- You can create many objects from the same class
- Each object can store its own values
If you want a deeper explanation, see Python classes and objects explained or what an object is in Python.
Basic steps to create an object #
The usual process is:
- Define a class with
class ClassName: - Optionally add
__init__to set starting values - Create the object with
ClassName(...) - Save it in a variable like
user = User(...)
Here is the general pattern:
class ClassName:
def __init__(self, value):
self.value = value
my_object = ClassName("example")
In this code:
ClassNameis the classmy_objectis the variable that stores the new object"example"is passed into__init__
Create an object without starting values #
This is the simplest possible example.
class Car:
pass
my_car = Car()
print(type(my_car))
print(my_car)
Possible output:
<class '__main__.Car'>
<__main__.Car object at 0x...>
Why this example helps:
- The class can be empty except for
pass - You still create the object by calling the class name
- It shows the object creation step clearly
The important line is:
my_car = Car()
That creates one Car object and stores it in my_car.
Create an object with __init__ #
Most of the time, you will want to give the object some starting data.
__init__ is a special method that runs automatically when the object is created.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person("Ana", 30)
print(person1.name)
print(person1.age)
Output:
Ana
30
What is happening here:
__init__runs whenPerson("Ana", 30)is calledselfrefers to the object being creatednameandagereceive the values you pass inself.nameandself.agestore those values in the object
So this line:
person1 = Person("Ana", 30)
creates the object and gives it starting values.
If you want to learn more about this method, see the __init__ method in Python explained.
Access object data and methods #
After creating an object, you usually want to use its data or call its methods.
Use dot notation for both.
Access data #
class Person:
def __init__(self, name):
self.name = name
person = Person("Lina")
print(person.name)
Output:
Lina
Call a method #
class Person:
def __init__(self, name):
self.name = name
def say_hello(self):
print("Hello, my name is", self.name)
person = Person("Lina")
person.say_hello()
Output:
Hello, my name is Lina
Key points:
- Use dot notation like
person.name - Call methods with dot notation like
person.say_hello() - Each object keeps its own attribute values
- The object variable points to that specific object
If you want to build your own class first, see how to create a class in Python. If you want objects to do more, see how to add methods to a class in Python.
Common beginner mistakes #
These are some common problems when creating objects in Python.
Forgetting parentheses #
Wrong:
class Dog:
pass
my_dog = Dog
print(type(my_dog))
Here, my_dog refers to the class itself, not an object.
Correct:
class Dog:
pass
my_dog = Dog()
print(type(my_dog))
Passing the wrong number of arguments #
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog()
This causes an error because name is required.
Correct:
my_dog = Dog("Max")
If you see this kind of problem, read how to fix TypeError: missing required positional argument.
Forgetting self inside class methods #
Wrong:
class Dog:
def __init__(name):
self.name = name
Correct:
class Dog:
def __init__(self, name):
self.name = name
Inside instance methods, the first parameter should usually be self.
Using a variable before assigning the object #
Wrong:
print(my_dog.name)
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog("Max")
Create the object first, then use it.
Other common causes #
- Trying to use a class before defining it
- Writing
object = ClassNameinstead ofobject = ClassName() - Defining
__init__with parameters but not passing values - Misspelling the class name when creating the object
How this page differs from related pages #
This page focuses on the task of creating an object.
It does not try to fully explain all class design.
It stays practical and action-focused:
- Create an object
- Pass values into it
- Use its attributes and methods
For deeper learning, use these related pages:
- Python classes and objects explained
- The
__init__method in Python explained - How to create a class in Python
- How to add methods to a class in Python
Helpful debugging checks #
If object creation is not working, these quick checks can help:
print(type(my_object))
print(my_object)
print(my_object.__dict__)
help(ClassName)
What these do:
print(type(my_object))shows the object’s typeprint(my_object)shows the object representationprint(my_object.__dict__)shows the object’s stored attributeshelp(ClassName)shows information about the class
Example:
class Dog:
def __init__(self, name):
self.name = name
my_object = Dog("Max")
print(type(my_object))
print(my_object.__dict__)
Output:
<class '__main__.Dog'>
{'name': 'Max'}
FAQ #
What is the difference between a class and an object? #
A class is the template. An object is one instance created from that template.
Do I always need __init__ to create an object? #
No. You can create an object without __init__, but __init__ is useful for setting starting values.
Can I create more than one object from the same class? #
Yes. Each object is a separate instance and can have different data.
Example:
class Dog:
def __init__(self, name):
self.name = name
dog1 = Dog("Max")
dog2 = Dog("Bella")
print(dog1.name)
print(dog2.name)
Output:
Max
Bella
Why do I get a TypeError when creating an object? #
Usually because the arguments passed to the class do not match the __init__ parameters.
For example:
class User:
def __init__(self, username):
self.username = username
user = User()
This fails because username is missing.
See also #
- Python classes and objects explained
- The
__init__method in Python explained - How to create a class in Python
- How to add methods to a class in Python
- How to fix
TypeError: missing required positional argument - What an object is in Python
Next step: learn how classes are structured, then add methods so your objects can do useful work.