Created
June 22, 2013 12:39
-
-
Save liuzhe0223/5840729 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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