How much is set in stone?

Grant Griffin not.this at seebelow.org
Mon Nov 5 06:29:35 CET 2001


Jive Dadson wrote:
...
> Other languages have started life with the feature that the first
> occurrence of an identifier defines it.
...
> Visual Basic (and perhaps other Basics, so far as I know) implement a
> way to have your cake and yet not spoil the old cake.

Perl has an option like this.  However, I like the way Python does it
better.

In Perl, you can use an unitialized variable (unless you disallow that
via the option).  Then, unitialized variables get automatically
initialized to sensible "zero" or "empty" values on first use.  Whenever
I used the auto-initialize thing, I regretted it.  However, I found the
declaritive form to be awkward.

Python's system is basically "you must write to a variable before you
can read it".  This has the net effect of catching the kind of error you
refer to, in nearly all cases--and I haven't had a significant problem
with the cases that can get missed.

Therefore, this simple but powerful idea gives you nearly all of the
benefit of variable declarations, yet none of the baggage.

Also, to do automatic zeroing of undeclared variables, Perl has to use
special punctuation as a back-door way of declaring the type of the
variable.  Then, you have to carry along this punctuation as you _use_
the variable.

Again, I like Python's approach better.  Since there is no automatic
declaration, the types of variables are specified when you assign to
them:

	x = 1           # x is an integer
	x = 1.0         # x is a float
	x = []          # x is a list
	x = {}          # x is a dictionary

	print y         # oops!--"y" hasn't been assigned (declared) yet

wish-I'd-thought-of-that-ly y'rs,

=g2
-- 
_____________________________________________________________________

Grant R. Griffin                                       g2 at dspguru.com
Publisher of dspGuru                           http://www.dspguru.com
Iowegian International Corporation            http://www.iowegian.com



More information about the Python-list mailing list