learning python ...

hw hw at adminart.net
Thu May 27 14:59:47 EDT 2021


On 5/25/21 3:09 PM, Greg Ewing wrote:
> 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 would this matter when the variables are typeless?

Often times, it's not very practical to figure out the type of a value 
before assigning it to a variable ...

>> 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.

Works fine in perl (and you can have it without warnings if you want to, 
though that's not recommended):


perl -e 'use warnings; my $shadow; my $shadow; my $shadow';
"my" variable $shadow masks earlier declaration in same scope at -e line 1.
"my" variable $shadow masks earlier declaration in same scope at -e line 1.


"Mask" seems like a much better word for this than "shadow" ...


More information about the Python-list mailing list