new topics Flashcards
inheritance
- Inheritance provides code reusability to the programme because we can use an existing class to create a new class instead of creating it from scratch
2Here child class acquires the properties and can access all the data members and functions defined in the parent class and Child class also can provide its specific implementation to the functions of parent class
simple inh
multi lvl
multiple inh
Simple inheritance
cls derived-class(base class):
<class-suite>
(or)
cls derived-class(<basecls 1>\_\_\_\_<basecla>):
<class-suite>
**Multi level inheritance**
cls cls1:
<class-suite>
class cls2(cls1:
<class-suite>
cls cls3(cls2):
<class-suite>
Multiple inheritance
cls cls1(basecls1, basecls2, basecls3_ _ _):
<class-suite>
</class-suite></class-suite></class-suite></class-suite></class-suite></basecla></class-suite>
issubclass(sub, sup)
return true if the sp. class is subclass of the Super Class and false for vv
The isinstance (obj, class)
- checks The relationship between objects and classes
- Returns true if the first parameter(obj) Is the instance of second parameter(cls)
Method overriding
- When parent class method is defined in child class with some specific implementation then this concept is called as method overriding
- This scenario is used when different definition of parent class is declared in child class
eg:
cls animal:
def speak(self):
print(“speaking”)
class dog(animal):
print(“barking”) #decl in p-cls as speaking and in c-cls as barking—-method overriding
d=dog()
d.speak()
o/p:
barking
real life eg on Method overriding
class Bank:
def getroi(self):
return 10;
class SBI(Bank):
def getroi(self):
return 7;
class ICICI(Bank):
def getroi(self):
return 8;
b1 = Bank()
b2 = SBI()
b3 = ICICI()
print(“Bank Rate of interest:”,b1.getroi());
print(“SBI Rate of interest:”,b2.getroi());
print(“ICICI Rate of interest:”,b3.getroi());
o/p:
Bank Rate of interest: 10
SBI Rate of interest: 7
ICICI Rate of interest: 8
Abstraction
- Abstractions used to hide the internal functionality of the function from the users
- The uses only interact with the basic implementation of the function but the inner working is hidden
- Abstraction is used to hide the relevant data or class in order to reduce the complexity
- It also enhances the efficiency of the application
abs cls in py
- Abstraction can be achieved by abstract classes and interfaces
- A class that consists of one or more abstract method is called as abstract class
-
Abstract methods do not contain their implementation
4.** Abstract class can be inherited by the subclass and abstract method gets its definition in the subclass** - abstract classes are meant to be the blueprint of other classes they are used in designing large functions
- Python provides **abc module **to use abs in py progs
- abc works by decorating methods of base class as abstract
- We use @abstractmethod Decorator to define abstract method or if you don’t provide definition to the method it automatically becomes the abstract method
**9. An abstract class contain both Normal method and abstract method - We cannot create objects for abstract class**
syn:
from abc import ABC
class classname(ABC):
encapsulation
- wrapping data and methods that work with data in one unit
- This prevents data modification accidentally by limiting access only to variables and methods
- An object’s method Can change a variable’s value to prevent accidental changes these variables are called as private variables
protected datamem: The ones which can be accessed And modified by the class and in the derived class
- single underscore(_)
Private datamem: Cannot be accessed by anyone outside of the class or except the class in which it is declared
-double underscore(_ _)
polymorphism
- Having multiple forms
- Refers to use of same function name but with different signature for multiple types
eg:
pre-def:
print (len(“Javatpoint”))
print (len([110, 210, 130, 321]))
user-def:
def add(p, q, r = 0):
return p + q + r
print (add(6, 23))
print (add(22, 31, 544))
poly in cls
class xyz():
def websites(self):
print(“Javatpoint is a website out of many availabe on net.”)
def topic(self): print("Python is out of many topics about technology on Javatpoint.") def type(self): print("Javatpoint is an developed website.")
class PQR():
def websites(self):
print(“Pinkvilla is a website out of many availabe on net. .”)
def topic(self): print("Celebrities is out of many topics.") def type(self): print("pinkvilla is a developing website.")
obj_jtp = xyz()
obj_pvl = PQR()
for domain in (obj_jtp, obj_pvl):
domain.websites()
domain.topic()
domain.type()
lamda
- Lambda functions in python are anonymous functions imply they dont have a name
- TEF keyword is needed to create a typical functional python but We can also define unnamed function with the help of Lambda
keyword - this func accepts a any count of inputs but only evaluates and returns one output
syn:
identifier=lambda args:expr
calling:
print (indientifier(value))
here,
identifier is like a function name
args can be any number
expr–expr using those args
eg:
a=lambda x,y:(x*y)
print(a(3,4))
Difference between Lambda and Def function
- Infunction we need to declare a function with name and send args to it While executing def
- We’re also required to use return keyword to provide output function was invoked after big executed
- But in Lambda function during its definition itself a statement is included which is given as output
- Beauty of Lambda function is there a convenience we need not to allocate a Lambda expression to a variable because we can put it any place a function requested
filter() with lambda
- Fulton method accepts two arguments in python a function and an iterable such as list
- The function is called for the every item of the list, and a new iterable or list is returned that holds just those ele that returned true when supplied to function
eg:
list1=[1,2,3,4,5]
oddlist=list(filter(lambda num: (num%2!=0),list1))
print(oddlist)
map() with lambda
- A method and Lister passed in map()
- The function is executed for all the elements in the list and then a new list is produced the elements generated by given function for everyitem
- eg:
numbers_list = [2, 4, 5, 1, 3, 7, 8, 9, 10]
squared_list = list(map( lambda num: num ** 2 , numbers_list ))
print( ‘Square of each number in the given list:’ ,squared_list )