S
Steven D'Aprano
Python require declarations for local names, but if it did it would
probably use "local".
Oops, I meant *doesn't* require declarations. Sorry for the error.
Python require declarations for local names, but if it did it would
probably use "local".
You are absolutely correct in principle. But in practice, there are ten
bazillion C, Pascal, COBOL, and BASIC programmers who understand the word
"variable" to mean a named memory location, for every Smalltalk or Lisp
programmer who understands a "variable" as a name binding. So it's pure
weight of numbers thing.
The average Lisp programmer will be completely aware that "variable" can
mean various things, and take care to determine what the word means in
Python. She will immediately grok what we mean, even if she thinks that
the "no variables" part is just an affectation ("Heh, those wacky Python
dudes think they don't have variables!") but at least she'll understand
the name binding part.
On the other hand, the average C programmer is barely aware that there
are other languages at all, let alone that some of them differ from C in
semantics as well as syntax. So by emphasising the differences ("Python
has no variables? It has name bindings?") we increase the likelihood that
he'll learn the differences in semantics as well as syntax.
So, in a very practical sense, "Python has no variables, it has name
bindings" is completely wrong except in the sense that really matters:
Python's variables don't behave identically to C variables.
You are absolutely correct in principle. But in practice, there are ten
bazillion C, Pascal, COBOL, and BASIC programmers who understand the word
"variable" to mean a named memory location, for every Smalltalk or Lisp
programmer who understands a "variable" as a name binding. So it's pure
weight of numbers thing.
Python's
basic data types are immutable. At best we could say they are read-only
variables.
So no, saying Python doesn't have variables is not the same as saying C
doesn't have variables but only memory locations. They are different
concepts entirely, though on the surface they look similar.
Op 29-06-13 16:02, Michael Torrie schreef:
I don't understand why members of this list keep saying this. Sure the
variables in python behave differently than those in C and algol But they
behave similarly as those in smalltalk and lisp and I haven't seen
anyone claim that smalltalk and lisp don't have variables.
exactly that.
Without wanting to analyze it in too much depth now, I
would want a local keyword to allow me to know I was protecting my
variables, and a way to specify other scopes, without so much implied
scoping in non-intuitive ways...
Now everybody is gonna tell me how wrong I am, but you asked what I
want, and that's what keeps aggrevating me with python.
Nobody ever asks why Python doesn't let you sort an int, or take
the square of a list...
Huh? What language are you programming in? Python doesn't have implied
scoping in non-intuitive ways.
just to be ornery, you can sort an int:
'112345569'
And I suppose, depending on how you define it, you can square a list:
other expression, by itself. In other words, squaring isFrom Wikipedia, "a square is the result of multiplying a number, or
def f(x):
def g(y):
print(x)
x = y
Within g, the variable x is implicitly local,
which is non-intuitive
since without the assignment it would not be.
# The alternative for either program or people is a 1-pass + backtracking
process where all understandings are kept provisional until the end of the
body and revised as required. 2 passes are simpler.
Python's basic data types are not necessarily immutable. Lists and dicts
are not immutable. Being a high-level language, the idea of "primitives"
like int, double, float, etc from C doesn't really apply. A Python dict
is not made up from Python ints. Both int and dict are equally "basic".
exactly that. Without wanting to analyze it in too much depth now, I
would want a local keyword to allow me to know I was protecting my
variables, and a way to specify other scopes, without so much implied
scoping in non-intuitive ways...
Now everybody is gonna tell me how wrong I am, but you asked what I
want, and that's what keeps aggrevating me with python.
just to be ornery, you can sort an int:
'112345569'
And I suppose, depending on how you define it, you can square a list:
Or simply an explicit declaration of scope at the beginning of the
function definition.
One of the reasons I switched to Python was to not have to do that, or
hardly ever. For valid code, an new declaration is hardly needed. Parameters
are locals. If the first use of another name binds it (and that includes
import, class, and def), it is local. If the first use of does not bind it,
it had better not be local (because if it is, there well be an exception).
If there are branches, each should be consistent with the others. One should
only need two readings to understand and fix unbound local errors.
One of the reasons I switched to Python was to not have to do that, or
hardly ever. For valid code, an new declaration is hardly needed. Parameters
are locals. If the first use of another name binds it (and that includes
import, class, and def), it is local. If the first use of does not bind it,
it had better not be local (because if it is, there well be an exception).
If there are branches, each should be consistent with the others. One should
only need two readings to understand and fix unbound local errors.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.