Unit 2
Unit 2
In the above example, we created a class with the name Parrot with two attributes: name and age.
we created instances of the Parrot class. Here, parrot1 and parrot2 are references (value) to our new objects.
We then accessed and assigned different values to the instance attributes using the objects name and
the notation.
• Python Inheritance
• Inheritance is a way of creating a new class for using details of an existing class without
modifying it.
• The newly formed class is a derived class (or child class). Similarly, the existing class is a base
class (or parent class).
The output of the below code snippet is:
Here, dog1 (the object of derived class Dog) can access members of the base class Animal.
It's because Dog is inherited from Animal.
Python Encapsulation
Encapsulation is one of the key features of object-oriented programming. Encapsulation
refers to the bundling of attributes and methods inside a single class.
It prevents outer classes from accessing and changing attributes and methods of a class.
This also helps to achieve data hiding.
In Python, we denote private attributes using underscore as the prefix i.e single _ or
double __. For example,
In the above program, we defined a Computer class.
We used __init__() method to store the maximum selling price of Computer. notice in the code.
we have tried to modify the value of __maxprice outside of the class. However, since __maxprice is a private variable, this
modification is not seen on the output. c.__maxprice = 1000
As shown, to change the value, we have to use a setter function i.e setMaxPrice() which takes price as a parameter.
• Polymorphism
• Polymorphism is another important concept of object-oriented programming. It simply means more than one
form.
• That is, the same entity (method or operator or object) can perform different operations in different scenarios.
• Let's see an example, in next slide
• Python Generators
• In Python, a generator is a function that returns an iterator that produces a sequence of values when iterated
over.
• Generators are useful when we want to produce a large sequence of values, but we don't want to store all of
them in memory at once.
def generator_name(arg):
# statements
yield something
we can define a generator function using the def keyword,
but instead of the return statement we use the yield statement.
• Python Lambda Function:
• Lambda function is a small anonymous function. It can take any number of arguments but can
only have single expression.
• lambda arguments : expression
• x = lambda a : a + 10
print(x(5))
• Multiply argument a with argument b and return the result:
• x = lambda a, b : a * b
print(x(5, 6))
• x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
• Why Use Lambda Functions?
• The power of lambda is better shown when you use them as an anonymous function inside
another function.
• Say you have a function definition that takes one argument, and that argument will be multiplied
with an unknown number:
• def myfunc(n):
return lambda a : a * n
map() - Python's map() method applies a specified function to each item of an iterable (such as a list, tuple, or string)
and then returns a new iterable containing the results.
The map() syntax is as follows: map(function, iterable)
The first argument passed to the map function is itself a function, and the second argument passed is an iterable (sequence of elements)
such as a list, tuple, set, string, etc.
Example 1 - usage of the map():
1.# Using map() to square each element of the data list
2.data = [1, 2, 3, 4, 5]
3.
4.# Map function returns the map object
5.squares = map(lambda x: x*x, data)
6.
7.# Iterating the elements of the squares
8.for i in squares:
9. print(i, end=" ")
10.
11.# Also, we can convert the map object into a list
12.squares = list(map(lambda x: x*x, data))
13.print(f"Squares: {squares}")
Output:
1, 4, 9, 16, 25 Squares: [1, 4, 9, 16, 25]
Here, the map function takes each element one by one from the data starting from x = 1.
Each element is passed to the lambda function, returning its square.
And the returned value is stored in the map object (an iterable).
• filter() - The filter() function in Python filters elements from an
iterable based on a given condition or function and returns a new
iterable with the filtered elements.
• The syntax for the filter() is as follows: filter(function, iterable)
• Here also, the first argument passed to the filter function is itself a
function, and the second argument passed is an iterable (sequence of
elements) such as a list, tuple, set, string, etc.
• Example 1 - usage of the filter():
• You are given a list of integers and should filter the even numbers
from the list.
•
1.# Using filter() to filter even numbers from a list
2.data = [1, 2, 3, 4, 5]
3.
4.# The filter function filters the even numbers from the data
5.# and returns a filter object (an iterable)
6.evens = filter(lambda x: x % 2 == 0, data)
7.
8.# Iterating the values of evens
9.for i in evens:
10. print(i, end=" ")
11.
12.# We can convert the filter object into a list as follows:
13.evens = list(filter(lambda x: x % 2 == 0, data))
14.
15.# Printing the evens list
16.print(f"Evens = {evens}")