473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PEP 343, second look


After taking a break from following PEP 343 and it's associated PEPs, I
wanted to look at it again because it still seemed a bit hard to get my
mind around.

http://www.python.org/peps/pep-0343.html

A new statement is proposed with the syntax:

with EXPR as VAR:
BLOCK

Here, 'with' and 'as' are new keywords; EXPR is an arbitrary
expression (but not an expression-list) and VAR is a single
assignment target.


How is EXPR arbitrary? Doesn't it need to be or return an object that
the 'with' statement can use? (a "with" object with an __enter__ and
__exit__ method?)

And if so, what is the minimum 'with' class needed to create such an
object? It would need an __exit__ method, because there would be no
point if it didn't have one. Does it absolutely need an __enter__
method? Are there any uses that might not require an __enter__?

Presuming __enter__ is always needed, would this be a minimum class that
returns a 'with' object?

class With(object):
def __enter__(self):
pass
def __exit__(self):
pass
A 'with-generator' object would need __enter__ and __exit__ methods, and
a try-yield-finally generator. Is there a name for a 'with-generator'
object? (a witherator ?)

It's possible that a function may be used that returns an
'with-generator' object and would be used in the same way that range()
is used in 'for' statements.

func with_gen(func, *args, **args):
wg = WithGen()
wg.gen = func(*arg, **args)
return wg
Which would use a generic with-generator base class.

class WithGen(object):
def gen(self): # Should this raise an error
try: # if it does't get over ridden?
yield None
finally:
pass
def __enter__(self):
try:
return self.gen.next()
except StopIteration:
raise RuntimeError("generator didn't yield")
def __exit__(self, type, value, traceback):
if type is None:
try:
self.gen.next()
except StopIteration:
print "file closed by with statement"
return
else:
raise RuntimeError("generator didn't stop")
else:
try:
self.gen.throw(type, value, traceback)
except (type, StopIteration):
return
else:
raise RuntimeError("generator caught exception")
And used like this:

def opening(filename, mode):
f = open(filename, mode)
try:
yield f
finally:
f.close()

with with_gen(opening, "testfile", "w") as f:
f.write("test file")
This seems (to me) to be an easier to understand alternative to the
decorator version. The class could also be used as a base class for
constructing other 'with' class's as well as the with_template decorator.

Will this work or am I missing something? Any suggestions for a
different (better) name for the with_gen function?

Regards,
Ron

Jul 19 '05 #1
2 1710
Ron Adam <rr*@ronadam.com> writes:
A new statement is proposed with the syntax:
with EXPR as VAR:
BLOCK
Here, 'with' and 'as' are new keywords; EXPR is an arbitrary
expression (but not an expression-list)...


How is EXPR arbitrary? Doesn't it need to be or return an object that
the 'with' statement can use? (a "with" object with an __enter__ and
__exit__ method?)


That's not a syntactic issue. "x / y" is a syntactically valid
expression even though y==0 results in in a runtime error.
Jul 19 '05 #2
Paul Rubin wrote:
Ron Adam <rr*@ronadam.com> writes:
A new statement is proposed with the syntax:
with EXPR as VAR:
BLOCK
Here, 'with' and 'as' are new keywords; EXPR is an arbitrary
expression (but not an expression-list)...


How is EXPR arbitrary? Doesn't it need to be or return an object that
the 'with' statement can use? (a "with" object with an __enter__ and
__exit__ method?)

That's not a syntactic issue. "x / y" is a syntactically valid
expression even though y==0 results in in a runtime error.


The term 'arbitrary' may be overly broad. Take for example the
description used in the 2.41 documents for the 'for' statement.

"The expression list is evaluated once; it should yield an iterable object."
If the same style is used for the with statement it would read.

"The expression, (but not an expression-list), is evaluated once; it
should yield an object suitable for use with the 'with' statement. ... "

Or some variation of this.
Regards,
Ron


Jul 19 '05 #3

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

Similar topics

2
by: johnmann56 | last post by:
Hello. I have written a simple image viewer application using C# .NET. It only needs to display one image at a time. When a different program, in C, running on the same machine, does a...
30
by: Andante.in.Blue | last post by:
I just browsed through some of my Access links when I came across the Ten Commandments of Access (http://www.mvps.org/access/tencommandments.htm). Some of the points I heartily agree with (and...
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
2
by: johnmann56 | last post by:
Hello. I have written a simple image viewer application using C# .NET. It only needs to display one image at a time. When a different program, in C, running on the same machine, does a...
16
by: liking C lang. | last post by:
How to write a C programming that it may output the char one after one every other second? For example: char a="hello world!" output h,next second output e,next second output l...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.