Steven, There are dunderheads who will maliciously misuse things. Yes, we know what __add__ is supposed to do. But if someone codes a class with one that ignores addition to a collection if it has reached a maximum size, or does addition modulo 16 or makes it play a happy birthday tune, while subtracting, it may not be trivial for the innocent user of your code to make much sense of it.
There are documented IDEAS about what many of those dunder methods might mean but no enforcement. I remember an Abstract Algebra course I took where we often talked about operations like + and * in very abstract ways and came up with some fascinating meanings to the operations. An example might be the various definitions of multiplication for vectors or matrices. When you multiply, are you multiplying corresponding parts or are you multiplying a row times a column and summing the multiplications for each slot in the matrix? Or could multiplication be a tad more complex and require also taking a transpose first? Or perhaps getting a determinant or eigenvalues or eigenvectors? What does it mean to add "1" to a complex number or quaternion or octonion? The reality is that two people can often try to make a similar class and come up with different ideas and implementations. If I have a class representing people who RSVP for a party, might a PLUS of a person result in upping the party count by 2 since we assume they are bringing a date and if their presence requires another table, upping that count and so on? A simple __add__ can result in many things such as checking the added person for dietary needs and adjusting more stuff just because adding a person can be complex. You re not going to sucker me into discussing obfuscation today. Some people here got touchy last time. I am not defending PERL nor slamming Python. I am saying people who behave well will try to avoid writing code that is hard to read and may be understood in multiple ways. There are cute ways to do things in many languages. Last comment, in what some might call stream of consciousness. Python allows a function to return without an explicit return. Usually it returns None but I not in some generators it may throw an exception instead. The problem is that if you are not looking carefully at indentation, you might thing the statements that follow may be part of the same function. So my personal preference is to have an explicit return instead of letting it drop out of the function or at least a comment saying this is the end of the function. Similarly, many people won't close a file (I am not talking about in a with statement) or delete variables no longer in use, and depend on these things happening automatically eventually. There may be nothing wrong with that, especially for smaller programs. But a part of me appreciates when the scales are visibly balanced. But when overdone, as in a function where every statement is in its own try/catch even for problems very unlikely to happen, there is too muc detail to follow. Something in between seems more comfortable. So interrupting an algorithm to del no longer needed variables may not be best either. To each their own. -----Original Message----- From: Tutor <tutor-bounces+avigross=verizon....@python.org> On Behalf Of Steven D'Aprano Sent: Friday, December 14, 2018 12:36 AM To: tutor@python.org Subject: [Tutor] Obfuscated Python [was Long Lines techniques] On Thu, Dec 13, 2018 at 11:07:59PM -0500, Avi Gross wrote: > Python may claim to be straightforward but I can easily see ways to > fool people in python too with dunder methods or function closures or > decorators or ... Dunder methods shouldn't fool anyone. Each dunder method has a straightforward use, and most of the time you can guess that they do. __add__ implements the + operator, etc. Closures and decorators can be abused, as can anything. One can write obfuscated code in any language. Here's some amusing obfuscated Python: https://www.reddit.com/r/Python/comments/i1qgp/reduce_and_lambda_two_great_t astes_that_taste/ https://mail.python.org/pipermail/python-list/2013-October/658470.html Anyone else want to share some obfuscated Python code? -- Steve _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor