In Python, a class is a blueprint for creating objects. Objects are instances of classes and represent real-world entities with attributes (data) and behaviors (methods). This article explains how to define classes and create objects, along with examples.
A class is defined using the class
keyword. Here is a simple example:
class Person: pass
In this example, a class named Person
is defined. It is currently empty and does not contain any attributes or methods.
To create an object, you call the class as if it were a function:
person1 = Person() print(type(person1))
This creates an instance of the Person
class and stores it in the variable person1
. The type
function confirms that it is an object of the Person
class.
Attributes and methods can be added to a class to define its properties and behavior.
class Person: def __init__(self, name, age): self.name = name self.age = age person1 = Person("Alice", 30) print(f"Name: {person1.name}, Age: {person1.age}")
Here, the __init__
method initializes the name
and age
attributes when the object is created. The attributes are accessed using the dot operator.
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): return f"Hello, my name is {self.name}." person1 = Person("Alice", 30) print(person1.greet())
The greet
method defines a behavior for the Person
class. It can be called using the dot operator on an object.
Class variables are shared across all instances of a class, while instance variables are unique to each object.
class Person: species = "Human" # Class variable def __init__(self, name, age): self.name = name # Instance variable self.age = age # Instance variable person1 = Person("Alice", 30) person2 = Person("Bob", 25) print(f"{person1.name} is a {person1.species}.") print(f"{person2.name} is a {person2.species}.")
In this example, the species
attribute is a class variable, shared by all instances of the class. The name
and age
attributes are instance variables, unique to each object.
class Person: def __init__(self, name, age): self.name = name self.age = age person1 = Person("Alice", 30) print(f"Before: {person1.name}, {person1.age}") # Modifying attributes person1.name = "Alicia" person1.age = 31 print(f"After: {person1.name}, {person1.age}")
Attributes of an object can be modified directly using the dot operator.
Encapsulation allows you to restrict access to certain attributes or methods by using single or double underscores.
class Person: def __init__(self, name, age): self.__name = name # Private attribute def get_name(self): return self.__name person1 = Person("Alice", 30) print(person1.get_name())
Here, the __name
attribute is private and cannot be accessed directly. Instead, a getter method is provided to access it.
class
keyword.__init__
method initializes attributes when an object is created.Understanding how to create classes and objects is fundamental to object-oriented programming in Python. By defining attributes, methods, and using concepts like encapsulation, you can model real-world entities and their behaviors effectively in your code.