[ python-Feature Requests-1122279 ] Option to force variables to be declared

SourceForge.net noreply at sourceforge.net
Thu Feb 17 02:23:08 CET 2005


Feature Requests item #1122279, was opened at 2005-02-14 05:40
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1122279&group_id=5470

Category: None
Group: None
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Zac Evans (karadoc)
Assigned to: Nobody/Anonymous (nobody)
Summary: Option to force variables to be declared

Initial Comment:
My most common python programming error is to spell a
variable different ways in different parts of a
program. Often this generates no warnings, and is a
difficult problem to track down.

The Zen of Python says that 'Explicit is better than
Implicit'. I would like it if I could set an option so
that I had to explicitly declare variables before I can
use them. eg.
x = 5
would generate a warning, but
x = int()
x = 5
would not.

I do believe that explicit is better than implicit with
respect to programming; and I know that this feature
would save me a _lot_ of debugging on my larger python
projects.

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2005-02-16 20:23

Message:
Logged In: YES 
user_id=80475

This issue comes up every now and then but just goes with
the territory  because there is no straight-forward way to
implement the suggested behavior (in part because Python is
built around "duck typing").

My suggestions are to use PyChecker and to create variable
names that are less likely to be misspelled.  Good doctest
or unittest discipline will also save debugging time.  

----------------------------------------------------------------------

Comment By: Terry J. Reedy (tjreedy)
Date: 2005-02-16 20:09

Message:
Logged In: YES 
user_id=593130

x = int() does not 'declare' x.  It binds it to the result of 
calling int without any arguments, which is 0.
>>> int()
0
So it is the same as x = 0.

Various ideas about declarations and finding typos have been 
discussed on comp.lang.python and elsewhere.  You might 
review some of the c.l.p threads, possibly via Google.

----------------------------------------------------------------------

Comment By: Brett Cannon (bcannon)
Date: 2005-02-15 14:29

Message:
Logged In: YES 
user_id=357491

This will never get in as proposed for technical reasons.  What is the 
difference between ``x=5`` and ``x=int()``?  To the compiler they are 
just assignments to a variable, one just happens to involve a function 
call.  The only way to get the feature you want is to somehow have a 
more specific way of declaring variables, like a 'var' keyword or 
something and that will probably never go.

As Jim suggested, PyChecker is great for this kind of thing.  There are 
things in the works to help lead to PyChecker being added to the 
standard library so your problem should get a partial solution included in 
Python some time in the future.

----------------------------------------------------------------------

Comment By: Jim Jewett (jimjjewett)
Date: 2005-02-15 12:57

Message:
Logged In: YES 
user_id=764593

For various reasons, this won't soon get into the core.

Meanwhile, there are some tools like pychecker which may 
help.

I've also found that declaring __slots__ makes the errors 
surface a little bit faster, when I'm dealing with attributes.  
(The most common reason for me to need the variable 
name in widely separated places.)

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1122279&group_id=5470


More information about the Python-bugs-list mailing list