Те, кто в Python не первый день, хорошо знают, что на число можно умножить не только число, но и другие типы. Главное, чтобы у этих типов была реализация такой операции.
# list
>>> [1] * 3
[1, 1, 1]
# tuple
>>> (2, 3) * 3
(2, 3, 2, 3, 2, 3)
# string
>>> "A" * 3
"AAA"
Так работает полиморфизм стандартных типов. Интересно здесь то, что это сработает и в том случае, когда порядок операндов обратный. То есть int умножить на [тип].
# list
>>> 3 * [1]
[1, 1, 1]
# tuple
>>> 3 * (2, 3)
(2, 3, 2, 3, 2, 3)
# string
>>> 3 * "A"
"AAA"
Если хотите реализовать такое поведение в ваших классах то следует помнить два момента:
1. Если множитель справа, то вам нужно реализовать метод __mul__, наш класс это первый операнд, то есть слева.
myType * 3
2. Если множитель слева, то вам нужно реализовать метод __rmul__, наш класс это второй операнд, справа.
3* myType
Всё тоже самое можно делать и для других математических операторов. И если в этом примере действие и результат будут фактически одинаковыми, то бывают ситуации, когда это не так.
Например, при умножении матриц имеет значение порядок операндов.
Для других операторов, таких как деление или сдвиг, очень важно кто с какой стороны находится.
>>> 2/4, 4/2
(0.5, 2.0)
>>> 2<<3, 3<<2
(16, 12)
>>> 100%15, 15%100
(10, 15)
#tricks#basic
#java#gale#java#keyi#minecraft#minecraft_performance#minecraft_server_software#mirai#paper
Leaf is a high-performance, customizable Minecraft server software based on Paper, designed to improve game speed and flexibility. It supports asynchronous pathfinding, mob spawning, and entity tracking, making gameplay smoother. Leaf is compatible with Spigot and Paper plugins, supports all username characters, fixes some Minecraft bugs, and uses a disk-saving file format. It integrates error tracking for easier server maintenance and keeps dependencies up-to-date. This means you get a faster, more stable Minecraft server with better plugin support and easier management, enhancing your overall gaming experience. Java 21 or higher is required to run it.
https://github.com/Winds-Studio/Leaf
🇯🇵 Elezioni #Giappone – Clamoroso exit poll di TBS: Il Partito Liberal Democratico della premier #Takaichi potrebbe da solo ottenere per la prima volta nella storia la super maggioranza dei 2/3 per cambiare la Costituzione.
Governo: 356
#LDP (conservatori): 321
#Ishin (destra libertaria): 35
Opposizione: 109
#CRA (centro): 50
#DPFP (centrodestra): 29
#Sanseito (estrema destra): 11
#Mirai (democrazia digitale): 8
#JCP (comunisti): 3
Altri: 8
@UltimoraPolitics24
#go#coolq#cqhttp#cqhttp_mirai#go#go_cqhttp#golang#group_manager#mirai#mirai_bot#nonebot#onebot#onebot_plugin#onebot_sdk#plugin#qq#qq_bot#qqbot#qqrobot#websocket#zerobot
ZeroBot-Plugin is a comprehensive utility plugin collection for the ZeroBot chatbot framework, offering over 100 features across entertainment, management, and productivity categories. The system provides high-priority functions like chat management, sleep tracking, and group administration, alongside mid-tier features such as image generation, music streaming, and game simulations. Users benefit from flexible plugin control—enabling or disabling specific features per group—and dynamic loading capabilities that reduce program size. The platform supports multiple deployment methods, from pre-compiled releases to local compilation, making it accessible whether you prefer ready-to-use binaries or customized builds. With extensive command options, scheduled task triggers, and AI integration, ZeroBot-Plugin transforms group chat management into an automated, entertaining experience while maintaining user control over which features activate in specific communities.
https://github.com/FloatTech/ZeroBot-Plugin