learning python ...

Greg Ewing greg.ewing at canterbury.ac.nz
Tue May 25 09:09:08 EDT 2021


On 25/05/21 5:56 pm, Avi Gross wrote:
> Var = read in something from a file and make some structure like a data.frame
> Var = remove some columns from the above thing pointed to by Var
> Var = make some new calculated columns ditto
> Var = remove some rows ...
> Var = set some kind of grouping on the above or sort it and so on.

As long as all the values are of the same type, this isn't too bad,
although it might interfere with your ability to give the intermediate
results names that help the reader understand what they refer to.

A variable that refers to things of different *types* at different
times is considerably more confusing, both for a human reader and
for any type checking software you might want to use.

> How can you write a recursive function without this kind of variable shadowing? Each invocation of a function places the internal namespace in front of the parent so the meaning of a variable name used within is always backed by  all the iterations before it.

Um, no. What you're describing is called "dynamic scoping", and
Python doesn't have it. Python is *lexically* scoped, meaning that
only scopes that textually enclose the function in the source are
searched for names. Frames on the call stack don't come into it.

> So what if you suggest we allow re-use of names but WARN you. ... The first 50 places may be in other instances of the recursive function and you have already been warned this way 49 times

If this were to be done, the shadowing would be detected at compile
time, so you would only be warned once.

-- 
Greg


More information about the Python-list mailing list