I have two classes in python
- Drunk
- Usual Drunk
The usual drunk class inherits from the drunk and provides a new implementation for its move method as shown below
class Drunk:
def __init__(self,name):
self.name = name
def move(self,field,cp,dist=1):
if field.getDrunk().name!= self.name:
raise ValueError('Drunk not in the field!')
for i in range(dist):
#pt = CompassPt(random.choice(CompassPt.possibles))
field.move(cp,1)
class UsualDrunk(Drunk):
def move(self,field,dist=1):
cp = random.choice(CompassPt.possibles)
Drunk.move(self,field,CompassPt(cp),dist)
Now the usual drunk class has two methods named move but with different parameters.
So in this scenario is it overriding or overloading?
It’s overriding. Python does not support overloading.
That said, overriding a method with one that accepts different arguments, especially when it accepts fewer arguments than the base class method, is a Bad Idea. Consider:
If
some_drunkis a regularDrunk, this works. But if it’s aUsualDrunk, it’ll pass the value ofcpto thedistparameter, which is probably not what was intended.