Skip to content

Instantly share code, notes, and snippets.

@liuzhe0223
Created June 22, 2013 12:39
Show Gist options
  • Save liuzhe0223/5840729 to your computer and use it in GitHub Desktop.
Save liuzhe0223/5840729 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Pw @ 2013-06-22 19:56:30
class Test:
def count(self,n):
while n > 0:
yield n
n -= 1
def __enter__(self):
return self.count(9)
def __exit__(self, exception_type, exception_val, trace):
try:
self.obj.close()
except AttributeError:
print 'Not closable.'
return True
with Test() as t:
while True:
print t.next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment