Python Feature Request: Add the "using" keyword which works like "with" in Visual Basic

T

Terry Reedy

| self.setFixedSize(200, 120)
| self.quit = QtGui.QPushButton("Quit", self)
| self.quit.setGeometry(62, 40, 75, 30)
| self.quit.setFont(QtGui.QFont("Times", 18, QtGui.QFont.Bold))
| self.connect(self.quit, QtCore.SIGNAL("clicked()"), QtGui.qApp,
| QtCore.SLOT("quit()"))
|
| to be rewritten as:
|
| using self:
| __setFixedSize(200,120)
| __quit = QtGui.QPushButton("Quit", self)
| __using quit:
| ____setGeometry(62, 40, 75, 30)
| ____setFont(QtGui.QFont("Times", 18, QtGui.QFont.Bold))
| __connect(self.quit, QtCore.SIGNAL("clicked()"), QtGui.qApp,
| QtCore.SLOT("quit()"))

If you want to save typing, you are free to use 's' instead of 'self' as
the parameter name. No need to make a fairly major language change.

I do things like 'import math as m' to save repetition.

tjr
 
J

jamadagni

So, the gain is the loss of something different? If you say so.

My mistake - I should have said "no pain, no gain".
IMHO, the ability to find something quickly weighs much stronger
than needing to write 5 characters more.

Five characters more how many times?
 
S

Steven D'Aprano

Please check for sanity and approve for posting at python-dev.

In Visual Basic there is the keyword "with" which allows an object-
name to be declared as governing the following statements. For
example:

with quitCommandButton
.enabled = true
.default = true
end with

This is syntactic sugar for:

quitCommandButton.enabled=true
quitCommandButton.default=true

Which is very much like Pascal's with block.

This question has been asked before:

http://effbot.org/pyfaq/why-doesn-t-python-have-a-with-statement-like-some-other-languages.htm

Despite what the Effbot says, I believe that there is no ambiguity that
can't be resolved.

Specifying that names used in a using-block have a leading dot makes it
obvious to the compiler which names are shortened:

using longname:
x = .attribute # must be longname.attribute


If we forbid nested using-blocks, then all you need is a pre-processor to
change ".attribute" to "longname.attribute". There's never any ambiguity.

But if you want to be really ambitious, one might allow nested
using-blocks. Now the compiler can't resolve names with leading dots at
parse-time, and has to search namespaces at runtime, but that's no
different from what Python already does.


using longname:
using anotherlongname:
x = .attr

In this case, at Python has to determine at runtime which object has an
attribute "attr". If that sounds familiar, it should: that's exactly what
happens when you say instance.attribute: Python searches
instance.__dict__ then instance.__class__.__dict__, and any superclasses.

There is one slight ambiguity left: should Python search longname first or
anotherlongname? But that decision has come up before, for nested scopes
in functions. It seems obvious to me that Python should search deepest to
most shallow, the same way that function nested scopes work.

So the above nested block would be equivalent to:

try:
x = anotherlongname.attr
except AttributeError:
try:
x = longname.attr
except AttributeError:
raise UsingError('no such attribute')


One might even allow a construct like this:

using longname, anotherlongname:
x = .attr

In this case, the search resolution order would be from left to right,
that is, longname before anotherlongname.
 
C

Colin J. Williams

James said:
Please check for sanity and approve for posting at python-dev.

In Visual Basic there is the keyword "with" which allows an object-
name to be declared as governing the following statements. For
example:

with quitCommandButton
.enabled = true
.default = true
end with

This is syntactic sugar for:

quitCommandButton.enabled=true
quitCommandButton.default=true

This can be very useful especially in GUI programming when we have to
type the same object name in line-after-line. I personally found
having to type the word "self" umpteen times inside classes very
irritating. Such a beautiful language is Python, she should have this
good feature from VB too.

Now I hear that the word "with" is being discussed for a different
purpose in Py 3 as a result of a PEP and I don't want to conflict with
that. So I propose the word "using" as a replacement. This also is
similar to the C++ "using" keyword which exposes the members of a
namespace to access without specifying the namespace scope for each
reference. For example after giving "using namespace std;" I can
change all references to "std::cout" to "cout", which is similar to
what I am proposing for Python now.

Some thoughts about how this "using" statement should behave. The word
using should be followed by an object name and a colon indicating the
start of a block. The object named after "using" must determine the
context (or whatever the technical word is) of the of the statements
in that block.

self.setFixedSize(200, 120)
self.quit = QtGui.QPushButton("Quit", self)
self.quit.setGeometry(62, 40, 75, 30)
self.quit.setFont(QtGui.QFont("Times", 18, QtGui.QFont.Bold))
self.connect(self.quit, QtCore.SIGNAL("clicked()"), QtGui.qApp,
QtCore.SLOT("quit()"))

to be rewritten as:

using self:
__setFixedSize(200,120)
__quit = QtGui.QPushButton("Quit", self)
__using quit:
____setGeometry(62, 40, 75, 30)
____setFont(QtGui.QFont("Times", 18, QtGui.QFont.Bold))
__connect(self.quit, QtCore.SIGNAL("clicked()"), QtGui.qApp,
QtCore.SLOT("quit()"))

[I don't know whether usenet will retain my indenting, so I changed
the tabs to underscores.]

This context governing may need to be limited to the first applicable
member - so that in the above example "self" governs setFixedSize,
quit, quit and connect only in each sentence and quit (self.quit)
governs setGeometry and setFont only. (Point is that the parser should
not search for self.QtGui, self.self or self.QtCore in sentences 3 and
7, and self.quit.QtGui in sentence 6.)

Due to my absence of professional experience, my request may be
somewhat unpolished technical-wise, but I believe that this is a very
useful feature for Python and hence request the technically-
knowledgeable to reformat it as necessary. Thank you.

I like this one for some reason. Just the "using self" would save hella
typing in a lot of classes. I would favor a convention with leading dots
to disambiguate from other variables. This wouldn't conflict with, say,
floats, because variable names can't begin with a number.

James

Yes, I like the idea too. It has deeper roots than Visual Basic. In
Pascal, Nicklaus Wirth used "with" for record access.

It's an idea that can be used with any object which has attributes.
The value of an attribute could be a function or a class.

It's a pity that the word "with" was used for a context declaration -
PEP 343. On the other hand, I believe "using" has been suggested as
an alternative, that seems a reasonable alternative.

Colin W.
 
P

Paul Boddie

Please check for sanity and approve for posting at python-dev.

Technically, you can post it yourself to python-dev, but you'll just
get bounced back here to discuss it with us. ;-)
In Visual Basic there is the keyword "with" which allows an object-
name to be declared as governing the following statements. For
example:

with quitCommandButton
.enabled = true
.default = true
end with

This is how the discussion started for the current "with" statement,
although it ended up doing something somewhat different.

[...]
Now I hear that the word "with" is being discussed for a different
purpose in Py 3 as a result of a PEP and I don't want to conflict with
that.

The "with" keyword appears in 2.5 onwards.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top