Telegram Group & Telegram Channel
Что выведет этот код?

Когда мы выполняем операции вида:

left + right

Вызывается метод left.__add__, но если left.__add__ возвращает NotImplemented, то вызывается
right.__radd__, а если и этот метод возвращает NotImplemented, то выбрасывается ошибка TypeError.

В нашем случае:
x = MyInt и мы переопределили метод __add__, принтанется int __add__, но далее мы возвращаем NotImplemented что заставляет вызвать метод MyFloat.__radd__.
А MyFloat.__add__ не будет вызыван, т.к. всегда вызывается __add__ левого операнда и только __radd__ правого соответсвенно.

В итоге принтанется:

int __add__
float __radd__
10.0


#числа



tg-me.com/pythrone/11
Create:
Last Update:

Что выведет этот код?

Когда мы выполняем операции вида:


left + right

Вызывается метод left.__add__, но если left.__add__ возвращает NotImplemented, то вызывается
right.__radd__, а если и этот метод возвращает NotImplemented, то выбрасывается ошибка TypeError.

В нашем случае:
x = MyInt и мы переопределили метод __add__, принтанется int __add__, но далее мы возвращаем NotImplemented что заставляет вызвать метод MyFloat.__radd__.
А MyFloat.__add__ не будет вызыван, т.к. всегда вызывается __add__ левого операнда и только __radd__ правого соответсвенно.

В итоге принтанется:

int __add__
float __radd__
10.0


#числа

BY PyThrone




Share with your friend now:
tg-me.com/pythrone/11

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

The seemingly negative pandemic effects and resource/product shortages are encouraging and allowing organizations to innovate and change.The news of cash-rich organizations getting ready for the post-Covid growth economy is a sign of more than capital spending plans. Cash provides a cushion for risk-taking and a tool for growth.

At a time when the Indian stock market is peaking and has rallied immensely compared to global markets, there are companies that have not performed in the last 10 years. These are definitely a minor portion of the market considering there are hundreds of stocks that have turned multibagger since 2020. What went wrong with these stocks? Reasons vary from corporate governance, sectoral weakness, company specific and so on. But the more important question is, are these stocks worth buying?

telegram from us


Telegram PyThrone
FROM USA