Advertisements
Advertisements
प्रश्न
Predict the output of the following program. Also state which concept of OOP is being implemented?
def sum(x, y, z):
print "sum= ", x+y+z
def sum(a, b):
print "sum= ", a+b
sum(10, 20)
sum(10, 20, 30)
कोड लेखन
Advertisements
उत्तर
Python 3 update: print "..." must be written as print("...").
Trace:
- The second definition of
sum(a, b)overwritessum(x, y, z). - Therefore,
sum(10, 20)prints the sum of two values. sum(10, 20, 30)causes an error because the active definition accepts only two arguments.
sum= 30
TypeError: sum() takes 2 positional arguments but 3 were given
The program attempts to implement function overloading, a form of polymorphism, but Python does not support it through repeated function definitions.
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
