David Beazley <[email protected]> added the comment:
Some context: I noticed this while discussing (in a course) a programming
trick involving instance initialization and locals() that I'd encountered in
the past:
def _init(locs):
self = locs.pop('self')
for name, val in locs.items():
setattr(self, name, val)
class Spam:
def __init__(self, a, b, c, d):
_init(locals())
In looking at locals(), it was coming back in reverse order of method arguments
(d, c, b, a, self). To be honest, it wasn't a critical matter, but more of an
odd curiosity in light of recent dictionary ordering.
I could imagine writing a slightly more general version of _init() that didn't
depend on a named 'self' argument if order was preserved:
def _init(locs):
items = list(locs.items())
_, self = items[0]
for name, val in items[1:]:
setattr(self, name, val)
Personally, I don't think the issue Nathaniel brings up is worth worrying about
because it would be such a weird edge case on something that is already an edge
case. Returning variables in "lexical order"--meaning the order in which first
encountered in the source seems pretty sensible to me.
----------
nosy: +dabeaz
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue32690>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com