
On Tue, Jul 24, 2018, 9:09 AM Chris Angelico <rosuav@gmail.com> wrote:
x = Foo(cfg).user.profile x.food
Remember, these lines could be a very long way apart. You could pass 'x' to a function unrelated to the line of code that created it. And 'x.food' still has to have the magic. You can't mandate that the call to Coalesce be on the same line as the attribute access - Python doesn't work that way.
So your perfectly ordinary dot operator now does magic in addition to normal attribute access. See why it's a dangerous thing?
Yes, of course. That's why I would recommend best practice is to unbox or otherwise use the conditional value as close to the magic code as feasible. Likewise, as I noted a little while ago, 'x.food' could equally well be a property that executed arbitrarily slow, magical, obscure, or even malicious operations. Equally, 'x + y' could do absolutely anything if we define .__add__() or .__radd__() methods. Everything in Python is magical in that sense, but we should deliberately keep the magic constrained to the amount needed.