Hi can someone tell me why the below code shows the error, I think this should run fine -
from abc import ABC, abstractmethod
class DriveStrategy(ABC):
@abstractmethod
def drive_strategy(self):
pass
class NormalDrive(DriveStrategy):
def drive_strategy(self):
print("Normal Drive")
class SpecialDrive(DriveStrategy):
def drive_strategy(self):
print("Special Drive")
class Vehicle:
def __init__(self, drive: DriveStrategy):
self.drive = drive
def drive(self):
self.drive.drive_strategy()
class SUV(Vehicle):
def just_a_method(self):
print("from SUV")
class PassengerBus(Vehicle):
def bus_method(self):
print("From Bus")
safari = SUV(SpecialDrive())
safari.drive()
It shows
'SpecialDrive' object is not callable
Expecting that to print "Special Drive".
The problem of this code is in Class Vehicle
, the variable name self.drvie
is same with the function drive()
.
To solve this problem, you just need to use another name for the function. There is an example:
class Vehicle:
def __init__(self, drive: DriveStrategy):
self.drive = drive
def Drive(self):
self.drive.drive_strategy()