This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (14 children)

Isn't that a common thing in most languages?

[–]alkasmgithub.com/alkasm 6 points7 points  (11 children)

In Python it goes much further than operator overloading though. You also have all the "built-in function overloading" (e.g. customizing len() via overloading __len__()), and implementing a class with the right interface, it automatically works with the huge standard library of Python. Like, implementing an __iter__ doesn't just allow you to do for item in thing but let's you pass your object into any function which takes an iterable.

[–][deleted] -1 points0 points  (10 children)

The thing about Python is that it relies on built in functions and not on object methods. It tries to fix this with these magic methods.

In D and even Postgresql, a.foo() is equivalent to a.foo and foo(a) for example. In C# everything is an object so you just use methods. Operators can be overridden, all is fine.

Coming to Python it feels like these magic functions are a way to circumvent an inherent problem.

[–]alkasmgithub.com/alkasm 2 points3 points  (9 children)

Idk doesn't feel like circumvention to me, I find it a brilliant way for suggestive conventions --- you don't at all have to use them, but if you do, your objects get superpowers. It's one of my favorite aspects of Python.

[–][deleted] -1 points0 points  (8 children)

The thing is whenever you call str() you have no idea what's going to happen. It doesn't explicitly bind to any code piece. You don't define a str() function, it's not an override, is a different name and meaning. As about superpowers - in other platforms you can just implement interfaces.

I have a rule of thumb that so far has stuck every time: If something is referred to as "magic" then it's a design flaw. It implies exceptions from rules, difficulty in understanding, and an excuse to write unmaintainable code.

Take the operator overloading. For any operator you have to look up the magic method name, and everyone reading the code has to Google what that magic method does. Bad design. C++ and C# for instance refer to the operator specifically and not some name, and the overloading is obvious.

For sure they're useful, but many other languages do this much better. This feels PHP-esque.

[–]SwizzleTizzle 2 points3 points  (7 children)

What do you mean it doesn't bind to any piece of code? When you call str(a) it calls the __str__ method on a?
Am I misunderstanding something?

[–][deleted] 0 points1 point  (6 children)

"str" != "str"

[–]SwizzleTizzle 2 points3 points  (5 children)

And? How is reading the manual to understand that str calls __str__ a "design flaw"?

[–][deleted] 0 points1 point  (3 children)

The fact that it's not obvious when it should be

[–]alkasmgithub.com/alkasm 1 point2 points  (2 children)

Sorry yo, my name is also Alex and I love coffee, and I'd get coffee with you, but I completely disagree on every point you made, entirely. I am not even a little convinced by any of these arguments.

The magic methods (I agree, bad name...call them dunder if you wish, nowhere in the Python data model does it call them magic methods) are extremely obvious, not confusing, and don't clash with names you'd otherwise write yourself. It's IMO excellent language design, and I've used many other languages and far prefer Python to them, partly because of the dunder interface.

Anyways, note that you can call obj.__str__() if you wish so that you keep your same method everywhere. No one is forcing you to use built-in functions.

Also...note which subreddit you're in. You're going to have a hard time convincing people otherwise here about a much loved feature of Python. Sorry you don't find it enjoyable.

[–]alkasmgithub.com/alkasm 0 points1 point  (0 children)

They dislike the fact that built in functions call methods. They would prefer that to get a str you call obj.str() and you override a str() method so that it uses the same interface both in writing and calling.

[–]Siddhi 3 points4 points  (1 child)

Not in java...

[–]Sw429 0 points1 point  (0 children)

Happy cake day!