News for hackers. Stuff that matters.
Re:A Guide to Python's Magic Methods (2012) by kqr on Thursday August 09, 2018 @03:06PM (#17727401)
This is a great explanation and a must-read for Python developers. Once you know this stuff, you'll be a much stronger debugger.

But it's also worth throwing a warning out there: when you first learn about a new type of magic, you'll be tempted to use it everywhere. Be careful!

I can recognise a clear specific period in my Python work which was just after I had read this, and a bunch of things suddenly became implicit.

Re:A Guide to Python's Magic Methods (2012) by nomel on Thursday August 09, 2018 @09:53PM (#17730123)
> Once you know this stuff, you'll be a much stronger debugger.

As you suggest, more accurately, you'll require the next person who debugs your code to read this, making them much better at debugging your code.

I no longer use magic and completely avoid meta programming in python. It confuses everyone, is not needed (just use a boring factory function), and severely limits who can help you work on your code, for what? A few lines of less boilerplate here and there? "Succinctness"? Some sort of "elegance"? More likely, a DSL that nobody understands but you!

The more I write, and mostly the more I interact with other people, the more I realize that code needs to be boring. No magic. I think this is why go is succeeding: no magic.

Re:A Guide to Python's Magic Methods (2012) by rootusrootus on Thursday August 09, 2018 @11:01PM (#17730385)
100% agree. I figure this is a big part of what turns normal developers into senior developers, and lead developers. It's easy to code. What takes wisdom is 1) knowing what not to write, and 2) self control on how you write it. As a mentor I try to convey this to the fresh new supercoders joining our team.
Re:A Guide to Python's Magic Methods (2012) by florabuzzword on Thursday August 09, 2018 @03:23PM (#17727559)
My reaction to this, as someone who doesn’t use many of these on a regular basis (probably guilty of overusing __new__ though) is actually that I can imagine these causing some real problems. The operator ones seem particularly tempting and dangerous at the same time.
Re:A Guide to Python's Magic Methods (2012) by ubernostrum on Thursday August 09, 2018 @03:33PM (#17727668)
Operator overloading, in any language that supports it, has always been controversial, so Python doesn't really add anything new there.

The thing is, it's also incredibly useful at times. Language features that are "useful when you have the use case for it, confusing when you don't" tend to provoke that reaction.

Re:A Guide to Python's Magic Methods (2012) by int_19h on Thursday August 09, 2018 @05:32PM (#17728687)
In Python, though, operator overloading is not merely "supported". It's how all operators are defined, including built-in ones.

   >>> dir(123)
   ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', 
   '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__',
   '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__',
   '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', 
   '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', 
   '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', 
   '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', 
   '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', 
   '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 
   'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

   >>> (1).__add__
   <method-wrapper '__add__' of int object at 0x678C40A0>

   >>> (123).__add__.__doc__
   'Return self+value.'

   >>> (1).__add__(2)
   3
Which is a good thing, because it makes the whole arrangement a lot more consistent that languages in which this sort of thing is just magic associated with a specific primitive type (sometimes it's even inconsistent - e.g. in C#, + for ints and strings is magic, but + for decimal is an overloaded operator). In Python, all that magic is confined to the magic methods. Learn them once, and they work the same everywhere.
Re:A Guide to Python's Magic Methods (2012) by florabuzzword on Thursday August 09, 2018 @05:37PM (#17728743)
Oh yeah, don’t get me wrong. It’s cool as hell! I’m totally gonna use it for a GUI menu-building script interface I’m making right now :).

I have just never done it before, and I think this is the first time I have seen the concept, so the first thing that popped in my mind we’re all the things that can go wrong. But, also all the things that can go right.

Re:A Guide to Python's Magic Methods (2012) by Puer on Thursday August 09, 2018 @08:35PM (#17729774)
Could you give an example of how you would use these methods to debug versus other methods/approaches? I'd love to hear if you're willing to share--I program mainly in Python these days but Python's magic methods are definitely something I need to understand at a deeper level.
Re:A Guide to Python's Magic Methods (2012) by travisjungroth on Friday August 10, 2018 @01:36AM (#17730947)
Fluent Python has a chapter on these methods and I highly recommend it. Also, these are much better described as dunder methods (short for double underscore) than magic methods. There's really no magic to them.