473,804 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

feature suggestion

hi there.

i didnt know if i should post it at python-dev or here, so i'll start
here. i'd like to suggest a new language feature for python that allows
you to explicitly declare a variable.

as we all know, just doing
v = 5
declares a new variable named 'v'... but we are people, and we do make
typos... and it's very disturbing to find your program crashing after
two weeks of operation, over a NameError... because a certain branch in
your code, that was previously never taken, had finally been taken.

i've written several large-scale projects in python by now... i find it
excellent for automated testing and other infrastructural needs... but
mistyped variable-, function-, class-, and module- names been a PITA
for me ever since i've started to use python.

python is a scripting language after all, and should preserve its
scripting nature. but adding features that help you verify your python
code should also be included.

BASIC has the "OPTION EXPLICIT" feature that forces you to explicitly
declare all variables, and i see no reason why python should have a
similar mechanism.

either you start python with some commandline switch, or with a
language construct, you should be able to turn on explicit variable
declaration.

first proposal:
===============
import sys
sys.is_strict = True
decl("x")
x = 5
y = 6

StrictError: name 'y' not explicitly declared

the syntactic format can be debated, of course. here 'decl' is used as
a built-in function that adds the name 'x' to a list of
"explicitly-declared-variables", and when setattr is called, it makes
sure the variable's name is within this list.

this is also fit for dynamic code, but suffers from still having to
execute the code before it is enforced. problems will remain in things
like:

decl("x")
x = 5
if x == 6:
y = 7

second proposal:
=============== =
syntactic verification, at parsing time. for example:

my-prog.py:
-------------

##decl x
##decl y, z
x = 5
y = 6
z = 8
a = 6
when this program will be ran with the -strict option, like 6th line
will generate a SyntaxError: name 'a' not explicitly declared.

this strategy can verify all sorts of dead-branches, as demonstrated
above, but is not fit for dynamic code.

---

personally, i like the second proposal more, because it adds no new
language constructs, only a new commandline switch and meta-comments,
and is totally backwards compatible.


-Tomer

Jul 18 '05 #1
3 1461
flexibal wrote:
hi there.

i didnt know if i should post it at python-dev or here, so i'll start
here.
Good choice. Python-dev is not a location for you to propose work for
other people to do for you.
BASIC has the "OPTION EXPLICIT" feature that forces you to explicitly
declare all variables, and i see no reason why python should have a
similar mechanism. What you are looking for is "pychecker" -- use google and find that;
you will be able to find many of the code problems you talk about
here.
syntactic verification, at parsing time. for example:
my-prog.py:
-------------
##decl x
##decl y, z
x = 5
y = 6
z = 8
a = 6 ### causes and error


This is _not_ a syntax error, syntax can be checked locally, without
memory. "a = 6" is not a syntax error because you can tell exactly
valid what form the statement has, and the form of every expression
in the statement. "a = 6 5" _is_ a syntax error; there is no way to
interpret "6 6" as an expression, and there is no way to interpret
"<statement > 6" as a statement. So, even if your proposal were to
be implemented, it would probably be an "access to undeclared
identifier" error.
--Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #2
Do not miss the discussion topic/thread:

"Optional Static Typing" (from 12/23/2004)

The referral article is very interesting, even for a newbie.

Introduction of (optional) types will "legitimate " the Python language
even more, while it keeps the fast scripting nature unspoilt.

flexibal wrote:
hi there.

i didnt know if i should post it at python-dev or here, so i'll start
here. i'd like to suggest a new language feature for python that allows
you to explicitly declare a variable.

as we all know, just doing
v = 5
declares a new variable named 'v'... but we are people, and we do make
typos... and it's very disturbing to find your program crashing after
two weeks of operation, over a NameError... because a certain branch in
your code, that was previously never taken, had finally been taken.

i've written several large-scale projects in python by now... i find it
excellent for automated testing and other infrastructural needs... but
mistyped variable-, function-, class-, and module- names been a PITA
for me ever since i've started to use python.

python is a scripting language after all, and should preserve its
scripting nature. but adding features that help you verify your python
code should also be included.

BASIC has the "OPTION EXPLICIT" feature that forces you to explicitly
declare all variables, and i see no reason why python should have a
similar mechanism.

either you start python with some commandline switch, or with a
language construct, you should be able to turn on explicit variable
declaration.

first proposal:
===============
import sys
sys.is_stri ct = True
decl("x")
x = 5
y = 6


StrictError: name 'y' not explicitly declared

the syntactic format can be debated, of course. here 'decl' is used as
a built-in function that adds the name 'x' to a list of
"explicitly-declared-variables", and when setattr is called, it makes
sure the variable's name is within this list.

this is also fit for dynamic code, but suffers from still having to
execute the code before it is enforced. problems will remain in things
like:

decl("x")
x = 5
if x == 6:
y = 7

second proposal:
=============== =
syntactic verification, at parsing time. for example:

my-prog.py:
-------------

##decl x
##decl y, z
x = 5
y = 6
z = 8
a = 6
when this program will be ran with the -strict option, like 6th line
will generate a SyntaxError: name 'a' not explicitly declared.

this strategy can verify all sorts of dead-branches, as demonstrated
above, but is not fit for dynamic code.

---

personally, i like the second proposal more, because it adds no new
language constructs, only a new commandline switch and meta-comments,
and is totally backwards compatible.


-Tomer

Jul 18 '05 #3
flexibal wrote:
hi there.

i didnt know if i should post it at python-dev or here, so i'll start
here. i'd like to suggest a new language feature for python that allows you to explicitly declare a variable.

as we all know, just doing
v = 5
declares a new variable named 'v'...
Does it? Who other than yourself comprise the "we" to whom you refer?
but we are people, and we do make
typos... and it's very disturbing to find your program crashing after
two weeks of operation, over a NameError... because a certain branch in your code, that was previously never taken, had finally been taken.
Google for "code coverage".

i've written several large-scale projects in python by now... i find it excellent for automated testing and other infrastructural needs
You need to test that your testing equipment works.
... but
mistyped variable-, function-, class-, and module- names been a PITA
for me ever since i've started to use python.

python is a scripting language after all, and should preserve its
scripting nature. but adding features that help you verify your python code should also be included.

BASIC has the "OPTION EXPLICIT" feature that forces you to explicitly
declare all variables, and i see no reason why python should have a
similar mechanism.
Most Python developers would agree with what you wrote, but not with
what the context shows you meant. Have you ever considered reading what
you have written before submitting it for execution? Google for "desk
checking".

either you start python with some commandline switch, or with a
language construct, you should be able to turn on explicit variable
declaration.

first proposal: [snip] second proposal:
=============== =
syntactic verification, at parsing time. for example:

my-prog.py:
-------------

##decl x
##decl y, z
x = 5
y = 6
z = 8
a = 6
when this program will be ran with the -strict option, like 6th line
will generate a SyntaxError: name 'a' not explicitly declared.


and also SyntaxError: name 's' declared but never used

Your proposal needs some augmentation:

(1) Have an environment variable DWIM_LEVEL -- if the edit distance
(with variable substitution penalties depending on keyboard proximity)
between a "not declared" name and a "declared but not used" name is
within the requested tolerance, then Python should assume equivalence
and continue silently.

(2) Recursively, this should be applied to the environment variable
itself, lest one type (say) SWIM_LEVEL or DIM_LEVEL.

(3) Have an ALTERNATE_METHO D_NAME option to cater for known problems
like "center" vs "centre", "-ise" vs "-ize", etc
I look forward to the discussion of your PEP.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2135
by: Jordan Rastrick | last post by:
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been using Python for a grand total of about a fortnight now. Hence, I apologise if what follows is a stupid suggestion, or if its already been made somewhere else, or if this is not the appropriate place to make it, etc. But I did honestly do some...
18
2226
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
32
4160
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open Source" manner, and in an attempt to build a ground-swell of support to convince the folks at Microsoft to add it. Proposal: "first:" "last:" sections in a "foreach" block The problem: The foreach statement allows iterating over all the...
8
30421
by: John | last post by:
Hi, I am developing an application using VB.Net and hope that the textbox can process features which are similar to auto-complete features in Window. For example, when user types "ap" in a textbox and the data associated to the textbox contains ("apple", "applet", "application"......), the application will suggest "apple" to the user to select and user can either select the suggestion or continue to type another word he disires. How...
1
1092
by: Fiander | last post by:
I dont know if this is the correct place to ask, but i'll just try. Will the dotnet framework be getting new native datatypes, like the vector or something like that ? for many calculations ( primarely grafics, audio, and academic alculations ), i now have to make a struct of 4 ints/floats, and create my own calculations for them. My guess is, that with a native type vector, the jit compiler would be able to use 3dnow/sse/sse2/sse3 on...
34
2327
by: samjnaa | last post by:
This is like the previous one. Please check for sanity and approve for posting at python-dev. I would like to have something like "option base" in Visual Basic. IIRC it used to allow me to choose whether 0 or 1 should be used as the base of member indices of arrays. In Python, the same can be used with strings, lists, tuples etc. This would mean: foo = "foo"
20
1571
by: Luke R | last post by:
One thing i used to use alot in vb6 was that tiny little button in the bottom left of the code window which allowed you to view either the whole code file, or just the function/sub you are currenly in. This was awesome when dealing with big functions inside big code files. You could page up and down without accidentally entering the next/previous function. Then they got rid of it in .NET and now we have this treeview style collapsable...
2
1130
by: Scott M. | last post by:
A suggestion that C# ASP .NET projects in Visual Studio should give the developer the ability to see all the Page object's events in the code editor (via the drop down lists at the top) and create an appropriate event handler automatically (like is done in VB .NET) has been submitted to Microsoft. Please visit the "connect" (https://connect.microsoft.com/default.aspx) site and vote for this suggestion if you like the idea. The id of...
1
1035
by: Scott M. | last post by:
A suggestion that C# ASP .NET projects in Visual Studio should give the developer the ability to see all the Page object's events in the code editor (via the drop down lists at the top) and create an appropriate event handler automatically (like is done in VB .NET) has been submitted to Microsoft. Please visit the "connect" (https://connect.microsoft.com/default.aspx) site and vote for this suggestion if you like the idea. The id of...
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.