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


PyThrone Telegram | DID YOU KNOW?

Date: |

The Singapore stock market has alternated between positive and negative finishes through the last five trading days since the end of the two-day winning streak in which it had added more than a dozen points or 0.4 percent. The Straits Times Index now sits just above the 3,060-point plateau and it's likely to see a narrow trading range on Monday.

What Is Bitcoin?

Bitcoin is a decentralized digital currency that you can buy, sell and exchange directly, without an intermediary like a bank. Bitcoin’s creator, Satoshi Nakamoto, originally described the need for “an electronic payment system based on cryptographic proof instead of trust.” Each and every Bitcoin transaction that’s ever been made exists on a public ledger accessible to everyone, making transactions hard to reverse and difficult to fake. That’s by design: Core to their decentralized nature, Bitcoins aren’t backed by the government or any issuing institution, and there’s nothing to guarantee their value besides the proof baked in the heart of the system. “The reason why it’s worth money is simply because we, as people, decided it has value—same as gold,” says Anton Mozgovoy, co-founder & CEO of digital financial service company Holyheld.

PyThrone from tr


Telegram PyThrone
FROM USA