Please post answer who is expert in Python!! so I wanna answers of Python OOP ex
ID: 3876196 • Letter: P
Question
Please post answer who is expert in Python!! so I wanna answers of Python OOP experts
Explain the following code how its work step by step then show what its print out but DONT POST ONLY OUTPUT as I said explain step by step
class Parent: def -init-(self,x,y): self. _x-x self. v-v def run (self): print ("x=O, y=0".format (self. x, self. class A (Parent): def runíself): print("f)".format(self. x + self. y)) class B (Parent): def runíself): print ("{}", format (self.x self._T)) class C (Parent): def runíself): print ("{}", format (self.x self._T)) L= [A(2,3), B(2,5), C("M",3), Parent (4,5)] for e in L e. run)Explanation / Answer
output:
Explanation:
first understand these terms, class,object, constructor and inheritance
Class — A blueprint created by a programmer for an object.
syntax - class class_name :
Object — An instance of a class.
syntax - obj = class_name(parameters)
Constructor — constructor is also called as initialization method, used to initialize values for class variables.
syntax - def __init__(self,parameters) :
Inheritance — It refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class.
synttax - class class_name(Parent_class) :
The string format() method formats the given string into a nicer output in Python.
now see the given code
class Parent: # Here class is defined with name Parent
def __init__(self,x,y): # constructor is defined
self._x=x # x value is assigned to class varialbe _x
self._y=y # y value is assigned to class varialbe _y
def run (self): # run function is defined with permision to access self/ class variables
print ("x={}".format(self._x,self._y)) # _x value will be printed out.
class A (Parent): # new class A is defined by inheriting Parent class
def run(self): # run method is defined
print ("x={}".format(self._x+self._y)) # result of _x+_y will be printed
class B (Parent): # new class A is defined by inheriting Parent class
def run(self): # run method is defined
print ("x={}".format(self._x*self._y)) # result of _x*_y will be printed
class C (Parent): # new class A is defined by inheriting Parent class
def run(self): # run method is defined
print ("x={}".format(self._x*self._y)) # result of _x*_y will be printed
L = [A(2,3),B(2,5),C("M",3),Parent(4,5)] # new list is created with instances of class A,B,C and Parent classes
for e in L: # for loop to traverse all the elements of list L
e.run(); # run() method of eache instance of the List is called
while creating list L, A class is called and construct of the Parent class will intialize values as _x =2 and _y =3
then B class is called and construct of the Parent class will intialize values as _x =2 and _y =5
then C class is called and construct of the Parent class will intialize values as _x ="M" and _y =3
then Parent class is called and construct of the Parent class will intialize values as _x =4 and _y =5
all intaces or objects stored in list L when we
for loop is exucuted run method of each object is called and out put is printed as expalined in comments of above code.
first A.run() is called and out put will be _x+_y is printed x = 5
then B.run() is called and out put will be _x*_y is printed x = 10
then C.run() is called and out put is printed as MMM because _x='M' and _y=3, when string is multiplied with integer then that string will be printed integer times. x = MMM
then Parent.run() is called and only _x is printed because format function prints one string only.
this is how the code is exucuted.
If you have any query please feel free to ask, i am very delighted to help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.