Tail recursion to while iteration in 2 easy steps

Terry Reedy tjreedy at udel.edu
Fri Oct 4 18:32:22 EDT 2013


On 10/4/2013 5:49 AM, Alain Ketterlin wrote:

> I think allowing rebinding of function names is extremely strange,

Steven already countered the 'is extremely strange' part by showing that 
such rebinding is common, generally useful, and only occasionally dodgy 
and a candidate for being blocked.

I want to consider here what it would mean to concretely implement the 
abstract notion 'disallow rebinding of function names' and show what 
would be behind calling the idea 'not feasible'.

1. A 'name' is not a 'function name' unless the name is bound, at 
runtime, to a 'function'.

2. A 'function' in Python is not just one class but any callable object 
-- with with a __call__ methods. That comprises an unbounded set.

3. Python does not mandate how namespaces are implemented. CPython uses 
both dicts and, for function local namespaces, internal C arrays. So 
'names' in code can become either string keys for dicts or integer 
indexes for arrays.

4. Rebinding can be explicit ('='), implicit ('import', 'class', 'def'), 
*or hidden* (globals('a') = 1; ob.__dict__('a') = 1). The 'hidden' 
methods are intentional as they are sometimes needed*.  In CPython, 
these forms remain different in the byte code, but it could be 
otherwise. The point is that is may or may not be possible for the 
interpreter to even recognize a 'rebinding' in order to apply any 
rebinding blocking rule.

* There is no trick (that I know of) for hidden rebinding of function 
locals and nonlocals (short of using ctypes), but I am not sure that 
this is a language guarantee. However, I an sure that the absence is 
intentional.

 > even though it's easier to implement.

No kidding.

-- 
Terry Jan Reedy




More information about the Python-list mailing list