Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

>> Polymorphism

The word polymorphism means having many forms. In programming, polymorphism means same function
name (but different signatures) being uses for different types.

>> Duck Typing

Duck Typing is a type system used in dynamic languages. For example, Python, Perl, Ruby, PHP, Javascript,
etc. where the type or the class of an object is less important than the method it defines. Using Duck Typing,
we do not check types at all. Instead, we check for the presence of a given method or attribute.

>> Operator Overloading

Operator Overloading means giving extended meaning beyond their predefined operational meaning. For
example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because
'+' operator is overloaded by int class and str class.

>> Method Overloading

In Python you can define a method in such a way that there are multiple ways to call it. Given a
single method or function, we can specify the number of parameters ourself. Depending on the function definition, it
can be called with zero, one, two or more parameters. This is known as method overloading.

>> Method Overriding

Overriding is the property of a class to change the implementation of a method provided by one of its base classes. ...
In Python method overriding occurs by simply defining in the child class a method with the same name of
a method in the parent class.

>> What do you understand by magic methods in Python?

Magic methods in Python are the special methods that start and end with the double underscores. They are also
called dunder methods. Magic methods are not meant to be invoked directly by you, but the invocation happens
internally from the class on a certain action.

>> Write Individual Programs for the following

Def__add__(self,Other):

class A:
def __init__(self, value=0):
self.value = value

def __add__(self, b):


return A(self.value + b)

def __str__(self):
return str(self.value)

a = A()
print(a + 1 + 2)

Def__sub__(self,Other):

class Point:

def __init__(self, x = 0, y = 0):


self.x = x
self.y = y

def __sub__(self, other):


x = self.x + other.x
y = self.y + other.y
return Point(x,y)

p1 = Point(3, 4)
p2 = Point(1, 2)
result = p1-p2
print(result.x, result.y) # prints (4,6)
Def__mul__(self,Other):

from numbers import Number


class MyFloat(object):
def __init__(self, a):
self.a = a

def __mul__(self, other):


return type(self)(self.a * other.a)

def __repr__(self):
return str(self.a)

class MyFloatExt(MyFloat):
def __init__(self, a):
super(MyFloatExt,self).__init__(a)

def __add__(self, other):


return type(self)(self.a + other.a)

def __mul__(self, other):


if isinstance(other,Number):
return type(self)(self.a * other)
else:
return super(MyFloatExt,self).__mul__(other)

a = MyFloatExt(0.5)
b = MyFloatExt(1.5)

c=a+b
print c

d=a*b
print d

e=d*c
print e

print isinstance(e, MyFloat)

f = e * 0.5
print f

print map(type,[a,b,c,d,e,f]) == [MyFloatExt]*6

Def__divmond__(self,Other):

class Test():
def __init__(self):
self._x=(1,2)
def __div__(self,div_fraction):
return (self._x[0]*div_fraction[1],self._x[1]*div_fraction[0])

y=Test()
z=y/(1,3)
print(z)

Def__gt__(self,Other):

# Python program to overload


# a comparison operators
 
class A:
    def __init__(self, a):
        self.a = a
    def __gt__(self, other):
        if(self.a>other.a):
            return True
        else:
            return False
ob1 = A(2)
ob2 = A(3)
if(ob1>ob2):
    print("ob1 is greater than ob2")
else:
    print("ob2 is greater than ob1")

Def__it__(self,Other):

class A(object):
def __init__(self, name, age, other):
self.name = name
self.age = age
self.other = other
def __cmp__(self, other):
assert isinstance(other, A) # assumption for this example
return cmp((self.name, self.age, self.other),
(other.name, other.age, other.other))

Def__le__(self,Other):

class B(object):
def __ge__(self, other):
print("__ge__ unexpectedly called")

class A(object):
def __le__(self, other):
print("__le__ called")

class AB(A, B):


pass

a = A()
ab = AB()

a <= ab # --> __ge__ unexpectedly called


ab <= a # --> __le__ called

You might also like