Python Notes 4 Th Unit
Python Notes 4 Th Unit
1. Encapsulation:
Encapsulation refers to bundling the data (variables) and the methods
(functions) that operate on the data into a single unit known as a class. It
also involves restricting access to some of the object's components (e.g.,
private variables or methods), so that the internal workings of an object are
hidden from the outside world.
2. Abstraction:
Abstraction is the concept of hiding the complex implementation details and
showing only the necessary features of an object. In Python, this is typically
done using classes and methods that expose a clean interface while hiding
the internal workings.
3. Inheritance:
Inheritance allows a new class (called a subclass) to inherit the properties
and behaviors (methods) from an existing class (called a superclass). This
promotes code reuse and the creation of hierarchical relationships between
classes.
4. Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a
common superclass. More specifically, it enables methods to have the same
name but behave differently depending on the object calling them. This is
achieved through method overriding (in subclass) or method overloading
(with different arguments in some languages, though Python relies more on
overriding).
5. Type Identification:
Type identification is the process of identifying the type of an object, which
helps in performing specific operations based on the object's type. In Python,
this can be done using the type() function or is instance() to check
an object’s type or class.
Classes in Python
A class in Python is a blueprint for creating objects (instances). A class defines the
properties and behaviors (methods) that the objects created from it will have.
Once the class is defined, you can create objects (instances) of that class. Each
object has its own set of attributes and can call the methods defined in the class.
Example:
python
Copy code
# Define a class
class Dog:
# Constructor
def __init__(self, name, age):
self.name = name
self.age = age
# Method
def bark(self):
return f"{self.name} says Woof!"
Instance Methods
An instance method is a method that operates on the instance of the class (the
object) and can access its attributes. These methods take self as their first
parameter, which refers to the instance of the class.
A class variable is a variable that is shared by all instances of the class. Unlike instance
variables, which are unique to each object, class variables are the same for all objects of that
class.
def info(self):
return f"{self.name} is {self.age} years old and belongs to the
{Dog.species} species."
# Create objects
dog1 = Dog("Max", 5)
dog2 = Dog("Bella", 3)
Inheritance
Inheritance allows one class to inherit the properties and methods of another class,
making code reuse easier. A subclass can override methods from the superclass to
provide its own specific implementation.
Example of Inheritance:
python
Copy code
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal): # Dog is a subclass of Animal
def speak(self):
return f"{self.name} barks."
# Create objects
dog = Dog("Rex")
animal = Animal("Generic Animal")
In this example, the Dog class inherits from Animal and overrides the speak
method.
Polymorphism
Example of Polymorphism:
python
Copy code
class Cat(Animal):
def speak(self):
return f"{self.name} meows."
# Create objects
cat = Cat("Whiskers")
dog = Dog("Rex")
Type Identification
To check the type of an object or identify its class, Python provides built-in
functions such as type() and isinstance().
Example:
python
Copy code
# Using type()
obj = Dog("Buddy", 5)
print(type(obj)) # Output: <class '__main__.Dog'>
# Using isinstance()
print(isinstance(obj, Dog)) # Output: True
print(isinstance(obj, Animal)) # Output: True (since
Dog inherits from Animal)