https://github.com/Rosuav/cpython/tree/pep-671

So uhh... anyone who knows about the internals of CPython and wants to
join me on this, I would *really* appreciate coauthors!

The implementation ended up a lot more invasive than I originally
planned. Some of that is inherent to the problem, but other parts
might be able to be done more cleanly. The way I've done it:

* Argument defaults (either in __defaults__ or __kwdefaults__) are now
tuples of (desc, value) or (desc,) for early-bound and late-bound
respectively
* Early-bound defaults get mapped as normal. Late-bound defaults are
left unbound at time of function call.
* For each late-bound default as of the 'def' statement, a check is
coded: if the local is unbound, set it based on the given expression.

This means that it's possible to replace an early-bound default with a
late-bound, but instead of actually evaluating the expression, it just
leaves it unbound:

>>> def f(x=1): print(x)
...
>>> f.__defaults__
((None, 1),)
>>> f.__defaults__ = ((None,),)
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in f
UnboundLocalError: cannot access local variable 'x' where it is not
associated with a value
>>>

I'm not entirely happy with this, but I also don't want to have too
much of a performance impact in the normal case.

So far unimplemented is the description of the argument default. My
plan is for early-bound defaults to have None there (as they currently
do), but late-bound ones get the source code. (In theory, it may be of
value to retain the source code for earlies too, which would allow hex
or octal integer literals to show up in help() as such, rather than
showing the (decimal) repr of the resulting value.) Anyone got good
pointers on how to do this, or is that likely to be impractical?

Feel free to criticize my code. As you'll see from the commit messages
in that branch, I have no idea what I'm doing here :)

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HFHO7LV4QSVFLWXJPQVSY2NPBBZACP5U/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to