-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Learning Python Design Patterns - Second Edition - Second Edition
By :
Now that we have understood the basics of object-oriented programming, let's dive into the major aspects of OOP.
The key features of encapsulation are as follows:
get and set.__ in the variable or function name.The major features of polymorphism are as follows:
+ operator can act on two integers to add them or can work with strings to concatenate themIn the following example, strings, tuples, or lists can all be accessed with an integer index. This shows how Python demonstrates polymorphism in built-in types:
a = "John" b = (1,2,3) c = [3,4,6,8,9] print(a[1], b[0], c[2])
The following points help us understand the inheritance process better:
In the following code example, class A is the base class and class B derives its features from class A. So, the methods of class A can be accessed by the object of class B:
class A:
def a1(self):
print("a1")
class B(A):
def b(self):
print("b")
b = B()
b.a1()The key features of abstraction are as follows:
In the following example, internal details of the Adder class are abstracted with the add() method:
class Adder:
def __init__(self):
self.sum = 0
def add(self, value):
self.sum += value
acc = Adder()
for i in range(99):
acc.add(i)
print(acc.sum)Composition refers to the following points:
In the following example, the object of class A is composited under class B:
class A(object):
def a1(self):
print("a1")
class B(object):
def b(self):
print("b")
A().a1()
objectB = B()
objectB.b()Change the font size
Change margin width
Change background colour