Message310933
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. |
|
Date |
User |
Action |
Args |
2018-01-28 10:34:44 | dabeaz | set | recipients:
+ dabeaz, gvanrossum, ncoghlan, njs |
2018-01-28 10:34:44 | dabeaz | set | messageid: <1517135684.15.0.467229070634.issue32690@psf.upfronthosting.co.za> |
2018-01-28 10:34:43 | dabeaz | link | issue32690 messages |
2018-01-28 10:34:43 | dabeaz | create | |
|