Код: Выделить всё
class Rectangle:
def __init__(self, length, width, **kwargs):
self.length = length
self.width = width
super().__init__(**kwargs)
def perform_hello(self):
def print_properties():
print(self.length, self.width)
Utility.hello(print_properties)
class Utility:
@staticmethod
def hello(f):
f()
if __name__ == '__main__':
Rectangle(2, 5).perform_hello() # outputs 2, 5
Я не уверен, как понимать это с объектно-ориентированной точки зрения, или, может быть, это так такое поведение Python я не совсем понимаю?
Если бы я попытался сделать то же самое «наивно», это, похоже, не сработало бы
Код: Выделить всё
class Square():
def __init__(self, length):
self.length = self.width = length
def perform_hello(self, f):
f()
if __name__ == '__main__':
f = print(self.length, self.width)
Square(4).perform_hello(f)
Подробнее здесь: https://stackoverflow.com/questions/792 ... ir-instanc