[Tutor] (no subject) [Python feature requests]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon Jun 9 23:15:01 2003


> > > <soapbox> perhaps this is a good place to mention (again) the
> > > desirability of a "with" statement in Python.</soapbox>
> >
> >How would a with statement help in Python? I've seen this request often
> >for C++ where it could be useful, but with Python's reference based
> >naming I've never felt a need for a with in Python?
> >
> >I usually just use 'it' as a handy shortcut to any long chains:
> >
> >it = foo.bar.baz.whatever
> >it.item = 42
> >it.another = 27
> >it.save()
> >
> >and so on...
> >
> >What exactly would a 'with' do that an 'it' doesn't?
>
> Having programmed in Pascal, Visual FoxPro and Visual Basic (all of
> which provide "with") I am accustomed to using with. So for me
>
> with foo.bar.baz.whatever:
>      .item = 42
>      .another = 27
>      .save()
>
> is more natural, more efficient, less error prone, more readable and
> more maintainable.

Hi Bob,

It does add some complication to the parser/lexer in Python, but that's
probably a minor technical point.  What complicates things is that the
introduction of 'with' will allow us to do things in two different ways,
and it's not quite obvious which way is better.  Let's take a look at both
of them again:


> >it = foo.bar.baz.whatever
> >it.item = 42
> >it.another = 27
> >it.save()

vs:

> with foo.bar.baz.whatever:
>      .item = 42
>      .another = 27
>      .save()


Since the first way, using a temporary variable, is already well
established in the language, the "underdog" 'with' needs to prove itself.
Does 'with' provide a compelling advantage over using a temporary
variable?  I'm not sure about this, and I'm definitely sure you'll get
resistance principally because the proposed fix is a syntax change.


> And what would it cost to add this feature to Python?

Very debatable.  Something probably best left debated on comp.lang.python.
*grin*


But whenever we think about introducing any new language feature, we
really do have to abandon the idea that this won't affect anyone else.
Is it likely that anyone else has used 'with' as a variable name?  Will
people have to modify their tools (like pychecker) to deal with this new
feature?  Does it complicate any rules that anyone else out there has been
using to pull out variable names?

Another important point: is 'with' nestable?  That is, when we run into
something like this:

###
with foo.bar.baz.whatever:
     .item = Megaboz()
     with .item:
         .another = 27
         .save()
###

what's the chance that someone reading this will get confused?  Will it
matter if Megaboz doesn't have a .save() method?


I'm trying to press the point: it's not immediately trivial to add a
"trivial" language feature.  It's not convincing enough to say, "Language
X has this feature; we should put it in Python.": it has to fit in with
the existing framework well enough so that it doesn't cause pandemonium.


That being said: Zope does use a "WITH" control feature in their DTML
templating language.  Because 'with' does have precedence with the Zope
folks; the Python core implementors know about it.  So it might not so far
fetched for Python to have 'with' in the future if it proves compelling.


Ultimately, if you want to get 'with' into the language, your best bet is
to propose it on comp.lang.python and write a PEP for it:

    http://www.python.org/peps/

By trial by fire, it'll either survive, or die.  It can't hurt (much) to
try.  At the most, you'll get a lot of heated responses.  *grin*


> One of my frustrations with proposing language enhancements are
> arguments from others that the proposal is not necessary.  Well neither
> is the for statement.  Everything it does can be implemented with the
> while statement.

We don't need loops either: everything can be done with function calls.
*grin*


> Yet someone deemed that a for statement "is more natural, more efficient
> and more readable."

Very true.  That someone (Guido van Rossum) is the one who decides what
becomes a part of the language or not.  And a language is spoken between
communities, so to get 'with' into Python, the feature has to be so good
that the community endorses it.  It's a hard task.


Talk to you later!