Forwarding the reply to the list. Accidentally pressed the wrong button and sent this message personally to Serhiy. Sorry :)
ср, 4 июл. 2018 г. в 11:57, Serhiy Storchaka <storch...@gmail.com>: > > if reductor := dispatch_table.get(cls): > > rv = reductor(x) > > elif reductor := getattr(x, "__reduce_ex__", None): > > rv = reductor(4) > > elif reductor := getattr(x, "__reduce__", None): > > rv = reductor() > > else: > > raise Error("un(shallow)copyable object of type %s" % cls) > > I was going to rewrite this code as > > reductor = dispatch_table.get(cls) > if reductor: > rv = reductor(x) > else: > rv = x.__reduce_ex__(4) > > There were reasons for the current complex code in Python 2, but now > classic classes are gone, and every class has the __reduce_ex__ method > which by default calls __reduce__ which by default is inherited from > object. With that simplification the benefit of using ":=" in this > example looks less impressed. > > Yes you are right with this particular snippet (in its current version, it is an echo from Python 2). But I think you have missed the general idea: The logic of this code is flat (like a switch) but the visual structure is pretty nested. That is why (maybe just for me) there is a mental imbalance in perception. And this type of code is rather common. Therefore, for me, the main gain from using the assignment expression in these patterns of code is that the visual syntax emphasizes the semantics. To be honest, I do not see any benefit of usage of assignment expression outside `if` and `while` headers, but if others do see let it be so. With kind regards, -gdg
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com