Warning: mkdir(): No space left on device in /var/www/tgoop/post.php on line 37

Warning: file_put_contents(aCache/aDaily/post/hw_code/--): Failed to open stream: No such file or directory in /var/www/tgoop/post.php on line 50
Hello World@hw_code P.605
HW_CODE Telegram 605
В каком то из давних интервью меня попросили предсказать вывод следующего Python-кода:


class A:
pass

class B:
value = 1

class C:
value = 3

class D(A, B, C):
def __str__(self):
return str(self.value)

print(D())


Эту проблему на тот момент я не мог решить, но я ее запомнил и вернулся к ней только недавно. Ключ к решению этой задачи лежит в понимании т.н. Method Resolution Order (MRO) в Python. MRO определяет порядок, в котором происходит поиск базовых классов при обращении к методу или атрибуту.

💡 В данном случае D наследуется от A, B и C. Но когда мы печатаем D(), атрибут value берётся из класса B! Почему?

Вот объяснение:
1️⃣ Python ищет value в классе D.
2️⃣ Затем он смотрит в A, но A не имеет value.
3️⃣ Далее он проверяет B — и находит value = 1.

Поскольку B стоит перед C в MRO, используется value из B, поэтому вывод будет 1! ⚙️

Вы можете увидеть MRO с помощью D.mro():

[<class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>]


@hw_code



tgoop.com/hw_code/605
Create:
Last Update:

В каком то из давних интервью меня попросили предсказать вывод следующего Python-кода:


class A:
pass

class B:
value = 1

class C:
value = 3

class D(A, B, C):
def __str__(self):
return str(self.value)

print(D())


Эту проблему на тот момент я не мог решить, но я ее запомнил и вернулся к ней только недавно. Ключ к решению этой задачи лежит в понимании т.н. Method Resolution Order (MRO) в Python. MRO определяет порядок, в котором происходит поиск базовых классов при обращении к методу или атрибуту.

💡 В данном случае D наследуется от A, B и C. Но когда мы печатаем D(), атрибут value берётся из класса B! Почему?

Вот объяснение:
1️⃣ Python ищет value в классе D.
2️⃣ Затем он смотрит в A, но A не имеет value.
3️⃣ Далее он проверяет B — и находит value = 1.

Поскольку B стоит перед C в MRO, используется value из B, поэтому вывод будет 1! ⚙️

Вы можете увидеть MRO с помощью D.mro():

[<class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>]


@hw_code

BY Hello World


Share with your friend now:
tgoop.com/hw_code/605

View MORE
Open in Telegram


Telegram News

Date: |

SUCK Channel Telegram Ng, who had pleaded not guilty to all charges, had been detained for more than 20 months. His channel was said to have contained around 120 messages and photos that incited others to vandalise pro-government shops and commit criminal damage targeting police stations. To view your bio, click the Menu icon and select “View channel info.” During a meeting with the president of the Supreme Electoral Court (TSE) on June 6, Telegram's Vice President Ilya Perekopsky announced the initiatives. According to the executive, Brazil is the first country in the world where Telegram is introducing the features, which could be expanded to other countries facing threats to democracy through the dissemination of false content. Hui said the messages, which included urging the disruption of airport operations, were attempts to incite followers to make use of poisonous, corrosive or flammable substances to vandalize police vehicles, and also called on others to make weapons to harm police.
from us


Telegram Hello World
FROM American