[EMAIL PROTECTED] wrote:
> Hello. Please tell me whether this feature request is sane (and not
> done before) for python so it can be posted to the python-dev mailing
> list. I should say first that I am not a professional programmer with
> too much technical knowledge.
> 
> I would like to have something like the "option explicit" statement in
> Visual Basic which turns on C-like checking for declaration of
> variables. This is highly helpful for people who are coming from C/C+
> +, for people who are learning programming using Python, and even
> professionals, since this helps prevent typo errors like:
> 
> sunlognitude = sunlongitude + 180.0
> 
> where the user has inadvertantly typed "g" before "n" and will later
> wonder why his application is not working as expected.
> 

I have no formal training in programming and thus have had to shake off 
all of the worst habits afflicting amateurs on my path to the modest 
proficiency I enjoy now. So let me assure you that this is something I 
can honestly say is just about the least of your worries and 
consequently has no place clogging up a language like python.

But, if you have masochistic tendencies and want a lot of overhead in 
your programming, you can always bind yourself mercilessly to classes:


class C(object):
   declared = ['bob', 'carol', 'ted', 'alice']
   def __setattr__(self, anattr, aval):
     if anattr not in C.declared:
       raise TypeError, "Just can't hook you up, bro."
     else:
       self.__dict__[anattr] = aval


E.g.:

py> class C(object):
...   declared = ['bob', 'carol', 'ted', 'alice']
...   def __setattr__(self, anattr, aval):
...     if anattr not in C.declared:
...       raise TypeError, "Just can't hook you up, bro."
...     else:
...       self.__dict__[anattr] = aval
...
py> c = C()
py> c.bob = 42
py> c.bob
42
py> c.x = 69
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 5, in __setattr__
TypeError: Just can't hook you up, bro.


Extending this ugliness to type-checking is left as an exercise for the 
reader. Using metaclasses for such nonsense is left as an exercise for 
metaclass maniacs.

James
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to