learning python ...

Michael Torrie torriem at gmail.com
Mon May 24 10:28:42 EDT 2021


On 5/24/21 8:17 AM, hw wrote:
> What does python actually do in the first example?  Does it overshadow a 
> variable or does it change one?  If it overshadows a variable, it would 
> be dubious, if it doesn't, it won't be dubious.

Are you referring to this?

      num = input("Enter a number: ")
      num = int(num)

No it is not "overshadowing" a variable.  You cannot get back to the
original string value for num.

> There are more alternatives:  Python might create a new variable with 
> the same name and forget about the old one.  Or it doesn't forget about 
> the old one and the old one becomes inaccessible (unless you have a 
> reference to it, if there is such a thing in python).  How do you call that?

Python variables are not memory boxes like in a compiled language.  They
are names bound to objects, as Mr Simpson alluded to.  So in the first
line, the name num is bound to a string.  In the second line, the name
is re-bound to an int object.  Furthermore, if num had come from the
global name scope, either of these lines would create a local name num
that does shadow the name from the global scope.

Hope that helps.




More information about the Python-list mailing list