def main(self):
self.__turn_on()
self.__move()
self.__distance_can_drive = self.__calculate_distance(self.__gas)
return self.get_current_speed
self.__distance_can_drive = self.__calculate_distance(self.__gas)он успешно вызвал класс метод и обновил переменную экземпляра.
@classmethod
def __calculate_distance(cls, gas):
""" Consumes 10 litres for 100 km. Returns the distance we can drive in km"""
return gas / 100
def __init__(self, vehicle_name: str, gas: int):
self.__gas = gas
def __init__(self, vehicle_name: str, gas: int):
self.vehicle_name = vehicle_name
self.gas = gas
self.distance_can_drive = None
def calculate_distance(self):
""" Consumes 10 litres for 100 km. Returns the distance we can drive in km"""
self.distance_can_drive = self.gas / 100
return self
class Car:
"""
Напрмер такой базовый паттерн, где всё надо инициализировать вручную:
c = Car("BMW")
c.turn_on()
c.move()
print(c.get_current_speed)
"""
def __init__(self, vehicle_name):
self.vehicle_name = vehicle_name
self.status = False
self.speed = None
def turn_on(self):
self.status = True
return self
def move(self):
if self.status:
self.speed = 100
return self
else:
return f"You forgot to turn on the engine. Turn it on using: {self.turn_on.__name__} method"
@property
def get_current_speed(self):
if self.speed:
return f"{self.vehicle_name} is moving at {self.speed} km/h."
else:
return f"Your current speed is 0 km/h. Drive the car it on using: {self.move.__name__} method"
class AnotherCar:
"""
И еще такой паттерн, где всё делает метод мейн (можно даже использовать "питонячее" сокрытие на пол шишечки используя синтаксис дандер методов чтобы было нагляднее):
a = AnotherCar("BMW")
print(a.main())
Ответ: BMW is moving at 100 km/h.
"""
def __init__(self, vehicle_name):
self.__vehicle_name = vehicle_name
self.__status = False
self.__speed = None
def __turn_on(self):
self.__status = True
return self
def __move(self):
if self.__status:
self.__speed = 100
return self
else:
return f"You forgot to turn on the engine. Turn it on using: {self.__turn_on.__name__} method"
def main(self):
self.__turn_on()
self.__move()
return self.get_current_speed
@property
def get_current_speed(self):
if self.__speed:
return f"{self.__vehicle_name} is moving at {self.__speed} km/h."
else:
return f"Your current speed is 0 km/h. Drive the car it on using: {self.__move.__name__} method"
def __repr__(self):
return (f"To move the car use: {self.main.__name__} method. "
f"To check the current speed use: {AnotherCar.get_current_speed.fget.__name__} property")
class AnotherCarExtended:
@classmethod
def __calculate_distance(cls, gas):
""" Consumes 10 litres for 100 km. Returns the distance we can drive in km"""
return gas / 100
def __init__(self, vehicle_name: str, gas: int):
self.__vehicle_name = vehicle_name
self.__gas = gas
self.__distance_can_drive = None
self.__status = False
self.__speed = None
def __turn_on(self):
self.__status = True
return self
def __move(self):
self.__speed = 100
return self
def main(self):
self.__turn_on()
self.__move()
self.__distance_can_drive = self.__calculate_distance(self.__gas)
return self.get_current_speed
@property
def get_current_speed(self):
if self.__speed:
return f"{self.__vehicle_name} is moving at {self.__speed} km/h."
else:
return f"Your current speed is 0 km/h. Drive the car it on using: {self.main.__name__} method"
@property
def get_distance(self):
if self.__distance_can_drive:
return f"Current gas will be enough for {self.__distance_can_drive} kilometres"
def __str__(self):
return (f"To move the car use: {self.main.__name__} method.\n"
f"To check the current speed use: {AnotherCarExtended.get_current_speed.fget.__name__} property.\n"
f"To check the distance we can drive use: {AnotherCarExtended.get_distance.fget.__name__} property.")
Написать полностью пошагово не могу займет очень много времени, но вот краткое описание.