Skip to content

Instantly share code, notes, and snippets.

@sloria
Created June 30, 2013 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sloria/5895687 to your computer and use it in GitHub Desktop.
Save sloria/5895687 to your computer and use it in GitHub Desktop.
f = open('file.txt', 'w')
f.write('hi')
f.close()
# Better
with open('file.txt', 'w') as f:
f.write('hi')
with pytest.raises(ValueError):
int('hi')
with SomeProtocol(host, port) as protocol:
protocol.send(['get', signal])
result = protocol.receive()
class SomeProtocol:
def __init__(self, host, port):
self.host, self.port = host, port
def __enter__(self):
self._client = socket()
self._client.connect((self.host,
self.port))
def __exit__(self, exception, value, traceback):
self._client.close()
def send(self, payload): ...
def receive(self): ...
@zaneb
Copy link

zaneb commented Feb 28, 2014

SomeProtocol.__enter__() is missing return self at the end. Without it, protocol on line 12 will be None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment