Classes in Python are blueprints for creating objects, encapsulating data and behavior. They define attributes and methods that instances of the class will have, allowing for organized and reusable code in object-oriented programming.
Instances are individual objects created from a class, each with its own set of attributes. By using classes and instances, programmers can model real-world entities and their interactions, making code more intuitive and easier to maintain.
Classes and Instances
Class definition with constructor
- Class serves as a blueprint or template for creating objects (an example of object-oriented programming)
- Defined using
classkeyword followed by class name (PascalCase convention) - Example class names:
Person,Car,BankAccount
- Defined using
__init__()is a special constructor method that initializes instance attributes- Called automatically when a new class instance is created
- Takes
selfas the first parameter, referring to the instance being created - Additional parameters can be defined to accept values for instance attributes
- Example:
def __init__(self, name, age)to initializenameandageattributes
- Example:
- Instance attributes are variables specific to each instance of a class
- Defined within
__init__()usingself.attribute_namesyntax - Each instance has its own copy of instance attributes
- Example:
self.name = nameandself.age = ageinside__init__()
- Example:
- Defined within
- Instance methods are functions defined within a class that operate on instances
- Defined like regular functions, but with
selfas the first parameter - Can access and modify instance attributes using
selfkeyword- Example:
def introduce(self): print(f"My name is {self.name} and I am {self.age} years old.")
- Example:
- Defined like regular functions, but with
Creation of class instances
- Instances are created by calling the class name followed by parentheses
- Arguments can be passed to
__init__()to initialize instance attributes- Example:
person1 = Person("Alice", 25)creates aPersoninstance withname"Alice" andage25
- Example:
- Arguments can be passed to
- Instance attributes can be accessed and modified using dot notation
- Syntax:
instance_name.attribute_name- Example:
person1.nameaccesses thenameattribute ofperson1
- Example:
- Syntax:
- Instance methods can be called on instances using dot notation
- Syntax:
instance_name.method_name(arguments)- Example:
person1.introduce()calls theintroducemethod onperson1
- Example:
- Syntax:
- Multiple instances of a class can be created, each with its own set of instance attributes
- Example:
person2 = Person("Bob", 30)creates anotherPersoninstance with different attribute values
- Example:
Instance vs class attributes
- Instance attributes are specific to each instance of a class
- Defined within
__init__()usingself.attribute_name - Each instance has its own copy of instance attributes
- Example:
self.nameandself.ageare instance attributes
- Example:
- Accessed and modified using dot notation on the instance:
instance_name.attribute_name- Example:
person1.name = "Alice"modifies thenameattribute ofperson1
- Example:
- Defined within
- Class attributes are shared by all instances of a class
- Defined directly within the class, outside of any methods
- All instances of the class have access to the same class attributes
- Example:
species = "Human"defined directly in thePersonclass
- Example:
- Accessed using dot notation on the class itself:
ClassName.attribute_name- Example:
Person.speciesaccesses thespeciesclass attribute
- Example:
- Can also be accessed through instances:
instance_name.attribute_name- Example:
person1.speciesalso accesses thespeciesclass attribute
- Example:
- Uses of instance attributes:
- Storing data specific to each instance (
name,age, etc.) - Representing the state or properties of an object
- Storing data specific to each instance (
- Uses of class attributes:
- Storing data shared among all instances of a class (
species, default values, etc.) - Defining constants or default values for the class
- Storing data shared among all instances of a class (
- Modifying class attributes affects all instances, while modifying instance attributes only affects the specific instance
- Example: Changing
Person.speciesaffects allPersoninstances, but changingperson1.nameonly affectsperson1
- Example: Changing
Object-Oriented Programming Concepts
- Encapsulation: Bundling data and methods that operate on that data within a single unit (class)
- Inheritance: Allows a class to inherit attributes and methods from another class
- Polymorphism: Ability of different classes to be treated as instances of the same class through a common interface