Adding optional 'explicit' declaration?

Laurent Pointal pointal at lure.u-psud.fr
Wed Apr 25 05:01:48 EDT 2001


The idea is to optionnaly allow to force an explicit variable declaration 
in Python functions/methods for compilation time check. 

The transparent creation of variables in Python is really nice, but there 
are times when it become a bug source, essentially because of misstyping of 
variable names.

What I would like to see is:

1) A __explicit__ directive.

2) A local directive, as there is the global directive, used to identify 
valid local names in functions/methods. 

With that, you could write things like:

    	__explicit__

    	userCount = 0
    	loglevel  = 3

    	def adduser(name) :
    	    	global userCount,loglevel
    	    	local userid,groupid
    	    	
    	    	userCount++
    	    	if loglevel>4 : print userCount,"users."
    	    	userid,groupid = get_userids(name)

If you misspelled loglevel in logLevel, or userid in userId, or name in 
Name... then Python compilation phase will abort with an error.

Ok, this dont resolve objects members/methods misstyping (hard to keep the 
dynamic side of Python without having things controlled only at execution 
time).

Note:
- extend to global code with a global declaration at module level?. 
- problem, this add a keyword in the langage which can break several code, 
maybe use __local__.


What do you think of this? 

Thanks.

Laurent.





More information about the Python-list mailing list