Singleton generators as code blocks

Discussion in 'Python' started by Dave Benjamin, Nov 22, 2003.

  1. Hey all,

    I was just reflecting upon the old code block debate, and the thought
    occurred to me that Python really does have a form of code blocks in its
    implementation of generators. It was a pretty natural conclusion, and I'm
    not sure why I didn't think of it before. For instance, compare Python's
    generator-style iteration versus Ruby's codeblock-style iteration:

    Python:
    for item in seq:
    print item

    Ruby:
    seq.each { |item|
    puts item
    }

    Now, aside from the difference in mechanics, these two methods basically
    accomplish the same thing. "seq" could be a regular list, or it could be a
    lazy sequence. The syntax remains the same.

    One of the simplest arguments in favor of codeblocks (or macros, but I'm not
    going to go there <grin>) was the ability to write a function like
    "with_open_file" that would open a file, perform some action on the file,
    and then close the file automatically. I'm going to use this as an example
    because of its simplicity, not because I think that there is an overwhelming
    need for such a function.

    In Python, I was able to implement a generator version quite simply. It
    seemed so obvious that I thought maybe there was a thread in the past where
    this was already mentioned; if so, forgive me for stating the obvious:

    def open_file(filename, mode='r'):
    f = file(filename, mode)
    yield f
    print 'closing...'
    f.close()

    for f in open_file('input.txt'):
    print f.read()

    The above code prints the conntents of the file "input.txt", then the string
    "closing...", and then closes the file.

    This seems to illustrate that generators and codeblock-accepting functions
    are really very similar, and that maybe codeblocks wouldn't really add much
    of anything to Python after all. I still have to wonder if maybe I'm missing
    something here...

    Has anyone used this sort of "singleton generator" technique?

    --
    ..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
    : d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
    Dave Benjamin, Nov 22, 2003
    #1
    1. Advertising

  2. Dave Benjamin

    Jp Calderone Guest

    On Sat, Nov 22, 2003 at 12:12:10AM -0000, Dave Benjamin wrote:
    > Hey all,
    >
    > [snip]
    >
    > In Python, I was able to implement a generator version quite simply. It
    > seemed so obvious that I thought maybe there was a thread in the past where
    > this was already mentioned; if so, forgive me for stating the obvious:
    >
    > def open_file(filename, mode='r'):
    > f = file(filename, mode)
    > yield f
    > print 'closing...'
    > f.close()
    >
    > for f in open_file('input.txt'):
    > print f.read()
    >
    > The above code prints the conntents of the file "input.txt", then the string
    > "closing...", and then closes the file.
    >
    > This seems to illustrate that generators and codeblock-accepting functions
    > are really very similar, and that maybe codeblocks wouldn't really add much
    > of anything to Python after all. I still have to wonder if maybe I'm missing
    > something here...
    >
    > Has anyone used this sort of "singleton generator" technique?


    This is a pretty nifty approach, but it seems to have (at least?) one serious
    drawback. If the body of the loop raises an exception, the generator is
    never resume, and the cleanup (or whatever) is never run.

    A way to reformulate the above without losing its conciseness eludes me
    currently.

    Jp
    Jp Calderone, Nov 22, 2003
    #2
    1. Advertising

  3. In article <>, Jp Calderone wrote:
    >> Has anyone used this sort of "singleton generator" technique?

    >
    > This is a pretty nifty approach, but it seems to have (at least?) one serious
    > drawback. If the body of the loop raises an exception, the generator is
    > never resume, and the cleanup (or whatever) is never run.


    Yeah, that is indeed a problem. That was in the back of my mind when I wrote
    that disclaimer (that it's just an example), but you summed it up very well.

    > A way to reformulate the above without losing its conciseness eludes me
    > currently.


    Me too. The obvious solution would be to wrap the body in a try/finally, but
    then, I guess we're right back where we started. ;)

    Thanks for the feedback.

    --
    ..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
    : d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
    Dave Benjamin, Nov 22, 2003
    #3
  4. Jp Calderone wrote:
    > This is a pretty nifty approach, but it seems to have (at least?) one serious
    > drawback. If the body of the loop raises an exception, the generator is
    > never resume, and the cleanup (or whatever) is never run.


    If open_file were implemented as a class rather than a
    generator, maybe the cleanup could be implemented with its
    __del__ method.

    Or maybe the iterator protocol should include a __cleanup__
    or __finally__ method that the for-loop invokes before leaving
    via a break or exception?

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
    Greg Ewing (using news.cis.dfn.de), Nov 24, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,214
    Proton Projects - Moin
    Mar 27, 2007
  2. matt
    Replies:
    1
    Views:
    240
    George Ogata
    Aug 6, 2004
  3. Wilhelm
    Replies:
    1
    Views:
    153
  4. Trans
    Replies:
    12
    Views:
    265
    Robert Klemme
    Sep 14, 2007
  5. Paul McMahon
    Replies:
    3
    Views:
    195
    David A. Black
    Jun 9, 2008
Loading...

Share This Page