most of the time it is very convenient that Python doesn't need variable declaration, but when writing larger programs it often happens to me that I mistype a variable name and thus inadvertently create wrong variables that can make debugging unnecessarily difficult. Therefore my question:
Is there a trick/feature/module that allows me to force variable declaration in python?
Many thanks,
Axel
> Is there a trick/feature/module that allows me to force variable
> declaration in python?
If by "variable declaration" you mean restricting the names of variables
allowed to a declared set of names, then you can do that using the
__slots__ attribute on new-style classes. More info here in this
previous UseNet thread:-
If you mean declaring the type of a variable, however, that's another
matter entirely.
HTH,
--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Use pychecker (http://pychecker.sourceforge.net)
HTH!
Heiko.
Yes, there's a trick:
def declare(declaration_list):
import sys
vars = sys._getframe(1).f_code.co_varnames
for v in vars:
if v not in declaration_list:
raise DeclarationError, "local variable '%s' not declared" % v
This function makes sure the function's local variables are all
present in the declaration list, otherwise it throws an exception.
DeclarationError is a homemade exception, by the way. Note that
declaration_list has to be a collection of strings.
*All* local variables appearing in the function, including the
arguments, must be declared in the declaration_list.
If you use this, I suggest you put this function inside an "if
__debug__". Here is an example of its use:
def function(a,b,c):
if __debug__:
declare(["a","b","c","hello","q","r","s"])
hello = 1
q = 2
...
But I recommend the use of PyChecker instead. It catches this and
many other errors, and is not intrusive.
--
CARL BANKS
> Is there a trick/feature/module that allows me to force
> variable declaration in python?
Heiko Wundram replies:
> Use pychecker (http://pychecker.sourceforge.net)
Heiko has already given you the answer you need, but I thought I
would elaborate some. Your problem, as you described it, is not
that you need the compiler to be aware of the types of the variables
(which is what variable declarations are all about). Your REAL
problem is having typos, which turn up as bugs. That's why Heiko
suggests that you use pychecker. It is a tool which reads over your
code looking for typos and lots of other sorts of "bugs" - things
that might be syntactically correct (like misspelling a variable)
but are almost certainly wrong.
I agree with Heiko: pychecker will help you in lots of ways (not
just with typos).
-- Michael Chermside
> If you mean declaring the type of a variable, however, that's another
> matter entirely.
One can also use the 'traits' package, available at scipy.org. It
allows you declare instance attributes with restricted types (and even
ranges, for example you can ask that an attribute be positive, or be
between 6 and 10, etc.)
http://www.scipy.org/site_content/traits/
--
Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)
Indeed, that is what I was looking for :-)
But I will also have a look at pychecker
Many thanks,
Axel