Two minor syntactic proposals

Jeff Shannon jeff at ccvcorp.com
Mon Jun 18 14:57:59 EDT 2001


philh at comuno.freeserve.co.uk (phil hunt) wrote in message news:<slrn9ipjok.5je.philh at comuno.freeserve.co.uk>...
> 
> I propose a different way of doing this: make 'self' implied in all
> references to instance variables. This involves definiing 2 new keywords,
> qclass and insvars. So:
> 
>    qclass Thing:
>       insvar v, w
>       def myMethod(p):
>          print p, v, x
>       def otherMethod():
>          myMethod(1)
>          someFunction()
> 
> would be equivalent to:
> 
>    class Thing:
>       def myMethod(self, p):
>          print p, self.v, x
>       def otherMethod(self):
>          self.myMethod(1)
>          someFunction()    
> 
> Note that a qclass definition would have to know what its methods and instance
> variables were, including those inherited from superclasses, for this to work.
> Is this feasible?

I much prefer having all member variables and member functions explicitly
qualified.  Having moved to Python from C++, where members can be referred
to implicitly, it seems to me that the explicit requirement of Python makes
code *much* easier to read--no more hunting about to try to figure out whether
a given variable is a class member, or a parameter, or a global, or from some 
other scope...  then there's also this problem (using your notation):

qclass Thing:
    insvar spam
    def foo():
        spam = 23

Does this modify the member variable Thing.spam?  Or does it create a 
new spam variable that is local to Thing.foo()'s scope?  I presume that
you'd want the former--but the latter behavior is more consistent with the
way that Python generally operates (i.e., builtins and globals are re-bound),
so having an exception for class members would be potentially confusing.

Jeff Shannon
Technician/Programmer
Credit International



More information about the Python-list mailing list