This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Option to force variables to be declared
Type: enhancement Stage:
Components: None Versions:
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, jimjjewett, karadoc, rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2005-02-14 10:40 by karadoc, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg54366 - (view) Author: Zac Evans (karadoc) Date: 2005-02-14 10:40
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
msg54367 - (view) Author: Jim Jewett (jimjjewett) Date: 2005-02-15 17:57
Logged In: YES 

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

Meanwhile, there are some tools like pychecker which may 

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.)
msg54368 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2005-02-15 19:29
Logged In: YES 

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.
msg54369 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2005-02-17 01:09
Logged In: YES 

x = int() does not 'declare' x.  It binds it to the result of 
calling int without any arguments, which is 0.
>>> int()
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.
msg54370 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-02-17 01:23
Logged In: YES 

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.  
Date User Action Args
2022-04-11 14:56:09adminsetgithub: 41575
2005-02-14 10:40:12karadoccreate