Professional Documents
Culture Documents
Day 23 Python Assignment
Day 23 Python Assignment
The word polymorphism means having many forms. In programming, polymorphism means same function
name (but different signatures) being uses for different types.
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 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.
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.
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.
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.
Def__add__(self,Other):
class A:
def __init__(self, value=0):
self.value = value
def __str__(self):
return str(self.value)
a = A()
print(a + 1 + 2)
Def__sub__(self,Other):
class Point:
p1 = Point(3, 4)
p2 = Point(1, 2)
result = p1-p2
print(result.x, result.y) # prints (4,6)
Def__mul__(self,Other):
def __repr__(self):
return str(self.a)
class MyFloatExt(MyFloat):
def __init__(self, a):
super(MyFloatExt,self).__init__(a)
a = MyFloatExt(0.5)
b = MyFloatExt(1.5)
c=a+b
print c
d=a*b
print d
e=d*c
print e
f = e * 0.5
print f
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):
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")
a = A()
ab = AB()