[Python-Dev] Re: closure semantics
Zack Weinberg
zack at codesourcery.com
Fri Oct 24 16:39:39 EDT 2003
Alex Martelli <aleaxit at yahoo.com> writes:
> On Friday 24 October 2003 12:27 am, Zack Weinberg wrote:
> ...
>> Frankly, I wish Python required one to write explicit declarations for
>> all variables in the program:
>>
>> var x, y, z # module scope
>>
>> class bar:
>> classvar I, J, K # class variables
>
> Seems like a great way to get uninitialized variables to me.
No, they get a magic cookie value that triggers an exception on use.
Which, incidentally, disambiguates the present UnboundLocalError - is
that a typo, or is that failure to initialize the variable on this
code path? Consider, eg.
def foo(x):
s = 2
if x:
a = 1
return a
...
> But then what added value is that 'classvar' boilerplate dirtying
> things up? Might as well take it off and get
> I = 2.3
> J = (2, 3)
> K = 23
>
> which is just what we have now.
...
>
> There is absolutely no help (not one minute later, not six months later)
> "comprehending" the program just because some silly language mandates
> redundancy, such as a noiseword 'classvar' in front of the assignments.
Understand that I do almost all my programming in typed languages,
where that keyword isn't noise, it's a critical part of the
declaration.
I see where you're coming from with regard to noisewords. There are
plausible alternatives, although they're all more complicated to
implement and explain, compared to
var a, b = 2, c = foo() # a throws UninitializedLocalError if used
# before set
...
d # throws UnboundLocalError
e = 1 # ALSO throws UnboundLocalError
But in this domain, I am mostly content with the language as is.
I think there really *is* a language deficiency with regard to
declaring class versus instance variables.
class foo:
A = 1 # these are class variables
B = 2
C = 3
def __init__(self):
self.a = 4 # these are instance variables
self.b = 5
self.c = 6
I find this imperative syntax for declaring instance variables
profoundly unintuitive. Further, on my first exposure to Python, I
thought A, B, C were instance variables, although it wasn't hard to
understand why they aren't.
People like to rag on the popularity of __slots__ (for reasons which
are never clearly spelled out, but never mind) -- has anyone
considered that it's popular because it's a way of declaring the set
of instance variables, and there is no other way in the language?
zw
More information about the Python-Dev
mailing list