[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "static" declaration
>>>>> "DW" == Dan Weinreb <email@example.com> writes:
DW> More broadly, the lack of declarations in Perl makes it a lot
DW> harder to read the code. For example, the code I am reading is
DW> very clearly object-oriented programming with classes and
DW> instances of classes in the utterly classical Smalltalk sense of
DW> those words. However, there is no place I can look to find the
DW> answer to the question "what are the instance variables of this
DW> class", since any imperative method anywhere is free to just add
DW> a new one at any time. So I am forced to trace through all the
DW> imperative paths in order to be able to draw my nice little
DW> notes in my notebook that say what the classes are and what
DW> their instance variables are.
I have been wondering, as this thread progresses, why do you want to
have a notes that say what the instance variables of classes are? I
don't mean to deride the need for these notes. Rather, I'm wondering:
Why I haven't been bothered by the lack of declarations for instance
variables in Python classes. Conversely, what about Perl makes the
lack of declarations a nuisance?
It seems that knowing the complete set of instance variables is, by
itself, rather uninteresting. If I want to understand how a method
works, the set of all instance variables isn't important. Some
instance variables won't be used in a method, and that fact isn't
Based on my very limited experience with Java, I expect that one
advantage of knowing all the instance variables of a class is
distinguishing between local variables and instance variables when
you're reading code. In Java method of more than a dozen lines, I
sometimes have trouble deciding what's an instance variable and what's
a local variable. In Python, instance variables are accessed as
attributes of self, so this is always explicit.
self._state = 
def eggs(self, arg):
self._state += arg
It's clear that eggs() is a method, because it is defined in a class.
_state must be an instance variable since it's an attribute of state,
while arg is a local variable. (It's also clear that eggs() isn't a
static or class method, because those are defined differently.)
There are other useful things in Java declarations -- the type of the
variable, its visibility, and, if needed, a comment explaining it's
purpose. In Python, I'd find some of this information in the class's
constructor. A typical Python class initializes instance variables in
the constructor, which provides a helpful place to hang the comments.
The comments are what I care about most. They can document intended
use, invariants, etc. that a type declaration doesn't capture.
For example, in our own Python projects, when we create a lock, we
document it's relationship to other locks and what instance variables
its associated with. The fact that lock1 must be acquired before
lock2, or that lock2 must be held when writing var1, is more
interesting than declaring that lock1, lock2, and var1 exist.