Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

forcing variable declaration ??

290 views
Skip to first unread message

Axel Kowald

unread,
May 27, 2003, 8:55:59 AM5/27/03
to
Hello everybody,

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

Alan Kennedy

unread,
May 27, 2003, 9:22:46 AM5/27/03
to
Axel Kowald wrote:

> 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:-

http://tinyurl.com/cr8k

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

Heiko Wundram

unread,
May 27, 2003, 9:13:36 AM5/27/03
to
On Tue, 2003-05-27 at 14:55, Axel Kowald wrote:
> Is there a trick/feature/module that allows me to force variable declaration in python?

Use pychecker (http://pychecker.sourceforge.net)

HTH!

Heiko.


Carl Banks

unread,
May 27, 2003, 2:27:38 PM5/27/03
to

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

Michael Chermside

unread,
May 27, 2003, 2:11:43 PM5/27/03
to
Axel Kowald writes:

> 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


Chad Netzer

unread,
May 27, 2003, 2:29:51 PM5/27/03
to
On Tue, 2003-05-27 at 06:22, Alan Kennedy wrote:

> 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)


Axel Kowald

unread,
May 28, 2003, 5:10:58 AM5/28/03
to
Hello Carl,

Indeed, that is what I was looking for :-)
But I will also have a look at pychecker

Many thanks,
Axel

0 new messages