Python - Objects
In Python, an instance of a class is created by calling the class as if it were a function. This process is known as instantiation. When a class is called, it creates an object (instance) of that class, which can then be used to access the attributes and methods defined in the class.
Creating an Instance:
- Use the class name followed by parentheses to call the class and create an instance.
- Pass any required parameters to the class constructor (__init__ method) during instantiation.
Let's illustrate how to create an instance of a class with a simple Python program:
# Define a class named 'Person'
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# Create an instance (object) of the 'Person' class
person_instance = Person(name="Alice", age=25)
# Access attributes of the created instance
name_of_person = person_instance.name
age_of_person = person_instance.age
In this example, we define a class 'Person' with an initializer (__init__ method) that sets the 'name' and 'age' attributes. We then create an instance ('person_instance') of the 'Person' class by calling the class and passing the required parameters.
We can access the attributes ('name' and 'age') of the created instance.
Output:
Name of the person: Alice Age of the person: 25
The key takeaway is that creating an instance involves calling the class and providing any necessary parameters. The instance retains its own set of attributes, independent of other instances of the same class.
In Python, a class is a blueprint or template for creating objects. It defines the structure and behavior that the objects will have. An instance, on the other hand, is a specific occurrence of an object created from a class. While a class describes the general characteristics, an instance represents a unique realization of those characteristics.
Difference between a Class and an Instance:
- Class: A class is a blueprint or template that defines the attributes and methods common to all instances of that class.
- Instance: An instance is a specific realization of a class, with its own set of attributes and values.
Let's illustrate the difference between a class and an instance with a simple Python program:
# Define a class named 'Car'
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
# Create two instances (objects) of the 'Car' class
car_instance1 = Car(make="Toyota", model="Corolla")
car_instance2 = Car(make="Honda", model="Civic")
# Access attributes of the created instances
info_car1 = f"Car 1: Make - {car_instance1.make}, Model - {car_instance1.model}"
info_car2 = f"Car 2: Make - {car_instance2.make}, Model - {car_instance2.model}"
In this example, we define a class 'Car' with an initializer (__init__ method) that sets the 'make' and 'model' attributes. We then create two instances ('car_instance1' and 'car_instance2') of the 'Car' class.
Each instance represents a unique car with its own make and model. Accessing the attributes of the instances provides specific information about each car.
Output:
Information about Car 1: Make - Toyota, Model - Corolla Information about Car 2: Make - Honda, Model - Civic
The key takeaway is that a class is a general template, while an instance is a specific occurrence based on that template. Instances have their own set of attributes, and changes to one instance do not affect others.
In Python, attributes are the properties or characteristics that define an object. They represent the data associated with an object and are defined within a class. Attributes are accessed using dot notation and can be variables or other objects.
Attributes in Python Objects:
- Attributes define the state of an object.
- They are declared within the class and represent the data associated with instances of the class.
- Attributes can be of various types, including integers, strings, or even other objects.
Let's illustrate the concept of attributes in Python objects with a simple program:
# Define a class named 'Person' with attributes 'name' and 'age'
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# Create an instance (object) of the 'Person' class
person_instance = Person(name="Alice", age=25)
# Access and display the attributes of the created instance
name_of_person = person_instance.name
age_of_person = person_instance.age
In this example, we define a class 'Person' with an initializer (__init__ method) that sets the 'name' and 'age' attributes. We then create an instance ('person_instance') of the 'Person' class.
The attributes 'name' and 'age' represent the state of the 'person_instance'. Accessing these attributes provides specific information about the person.
Output:
Name of the person: Alice Age of the person: 25
The key takeaway is that attributes define the data associated with an object and play a crucial role in representing the state of instances of a class.
In Python, both class attributes and instance attributes are used to represent data within a class. However, they serve different purposes and have distinct scopes.
Class Attributes:
- Class attributes are shared by all instances of a class.
- They are defined outside of any method in the class and are associated with the class itself, not with instances.
- Changes to class attributes affect all instances of the class.
Instance Attributes:
- Instance attributes are specific to each instance of a class.
- They are defined inside the class initializer (__init__ method) and are unique to each object created from the class.
- Changes to instance attributes only affect the specific instance where the change is made.
Let's illustrate the distinction between class attributes and instance attributes with a simple Python program:
# Define a class named 'Person' with a class attribute 'species' and instance attributes 'name' and 'age'
class Person:
species = "Homo sapiens" # Class attribute shared by all instances
def __init__(self, name, age):
self.name = name # Instance attribute specific to each instance
self.age = age # Instance attribute specific to each instance
# Create two instances (objects) of the 'Person' class
person_instance1 = Person(name="Alice", age=25)
person_instance2 = Person(name="Bob", age=30)
# Access and display both class and instance attributes
species_of_person1 = person_instance1.species
species_of_person2 = person_instance2.species
name_of_person1 = person_instance1.name
name_of_person2 = person_instance2.name
In this example, we define a class 'Person' with a class attribute 'species' and instance attributes 'name' and 'age'. The class attribute 'species' is shared by all instances, while 'name' and 'age' are specific to each instance.
We then create two instances ('person_instance1' and 'person_instance2') of the 'Person' class and access both class and instance attributes.
Output:
Species of Person 1: Homo sapiens Species of Person 2: Homo sapiens Name of Person 1: Alice Name of Person 2: Bob
The key distinction is that changes to class attributes affect all instances, whereas changes to instance attributes only affect the specific instance where the change is made.