coding style - where to declare variables

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jul 22 20:09:29 EDT 2018


On Mon, 23 Jul 2018 00:08:00 +0300, Marko Rauhamaa wrote:

> Would you call it binding in this case:
> 
>    X[0]["z"] = getit()
>    X[3]["q"] = X[0]["z"]
>    X[0]["z"].changeit()

It is a binding, but it is not a *name* binding. Since we are talking 
about name bindings, and comparing/contrasting them to variable 
assignment in classical languages, I don't think that binding to slots in 
hash tables or arrays is relevant except to muddy the waters and make 
things more complicated than they need be.


> I think what you are talking about is more usually called "referencing."

I don't think so. Its certainly not a term I've ever heard in this 
context before.


>> With a language with more ‘classical’ variable, the assignment of Y = X
>> would normal make a copy of that object, so the value Y does not get
>> changed by X.changeit().
> 
> I Java terms, all Python values are boxed. 

Correct. 

Java mixes two different models of variable assignment: it uses classical 
C- and Pascal-like variable assignment for primitive values, and Lisp- 
and Smalltalk-like name binding for boxed values (objects), leading to 
two distinct sets of behaviour. That makes Java a good lesson in why it 
is useful to distinguish between two models of name binding.

Java is not the only language with the distinction between "value 
types" (primitive values usually stored on the stack) and "reference 
types" (usually objects stored in the heap). C# and other .Net languages 
often make that distinction:

http://net-informations.com/faq/general/valuetype-referencetype.htm

Swift is another such language.

Other languages which use primarily or exclusively value-types (i.e. the 
"variables are a named box at a fixed memory location" model) include 
Algol, Pascal, Modula-3, C, C++, C#, Objective C, D, Swift, COBOL, Forth, 
Ada, PL/I, Rust and many others.



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list