5 ms·
It isn't the same operator overloading. In C++ operator overloading can easily mess with fundamental mechanisms, often intentionally; in Python it is usually n
by HelloNurse 1y ago
It isn't the same operator overloading.
In C++ operator overloading can easily mess with fundamental mechanisms, often intentionally; in Python it is usually no more dangerous than defining regular functions and usually employed purposefully for types that form nice algebraic structures.
- pjmlp 1y agoI hardly see the difference, given the capabilities of operator overloading in Python, class MyNum(int): def __add__ (self, other): return super().__add__(other) * 10 n = MyNum(12) a = 45 print(n + a) # oops
- HelloNurse 1y agoThis type confusion would have been identical with a plain function, __add__ is only syntactic sugar: class MyNum(int): def ten_times_sum (self, other): return (self+other)* 10 n = MyNum(12) a = 45 print(n.ten_times_sum(a)) Compare with, for example, fouling the state of output streams from operator>> in C++.
- pjmlp 1y agoHardly any different, trying to pretend Python is somehow better. Operator overload is indeed syntactic sugar for function calls, regardless of the language. By the way, you can overload >> in Python via rshift(), or __rshift__() methods.
- HelloNurse 1y agoOf course I can overload >> in Python, but I cannot foul up output stream state because it doesn't exist. Formally there is little difference between C++ and Python operator overloading and both languages have good syntax for it, but C++ has many rough edges in the standard library and intrinsic complications that can make operator overloading much more interesting in practice. For instance, overload resolution is rarely trivial.
- boxed 1y agoIt's the exact same thing except in Python the community largely has taste. In C++ `cout >> "foo"` exists in the standard library.
- quotemstr 1y agoI love how among a certain set the word "taste" has become an all-purpose substitute for having an argument or making a case. It basically means "I have more social media follows than you do, so I'm right"
- boxed 1y agoI believe the C++ community as a whole are quite convinced that overloading >> for stdout was a mistake.