[Python-Dev] Re: closure semantics

Phillip J. Eby pje at telecommunity.com
Fri Oct 24 17:10:03 EDT 2003


At 01:39 PM 10/24/03 -0700, Zack Weinberg wrote:
>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.

A, B, and C *are* instance variables.  Why do you think 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,

What good does declaring the set of instance variables *do*?  This seems to 
be more of a mental comfort thing than anything else.  I've spent most of 
my career in declaration-free languages, though, so I really don't 
understand why people get so emotional about being able to declare their 
variables.


>  and there is no other way in the language?

Actually, there are a great many ways to implement such a thing.  One way 
might be something like:

class RestrictedVars:
     vars = ()
     def __setattr__(self,attr,name):
         if name not in self.vars:
             raise AttributeError("No such attribute",attr)

class SomeClass(RestrictedVars):
     vars = 'a','b','c'




More information about the Python-Dev mailing list