Skip to content

Commit

Permalink
Converted "wb" to "w" and removed swear words :p
Browse files Browse the repository at this point in the history
  • Loading branch information
yasoob committed Aug 17, 2015
1 parent f96812e commit fe4b2b8
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions context_managers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Context managers allow you to do specifically that. For example:

::

with open('some_file', 'wb') as opened_file:
with open('some_file', 'w') as opened_file:
opened_file.write('Hola!')

The above code opens the file, writes some data to it and then closes
Expand All @@ -18,7 +18,7 @@ close it. The above code is equivalent to:

::

file = open('some_file', 'wb')
file = open('some_file', 'w')
try:
file.write('Hola!')
finally:
Expand Down Expand Up @@ -57,7 +57,7 @@ a ``with`` statement. Let's try:

::

with File('demo.txt', 'wb') as opened_file:
with File('demo.txt', 'w') as opened_file:
opened_file.write('Hola!')

Our ``__exit__`` function accepts three arguments. They are required by
Expand Down Expand Up @@ -89,8 +89,8 @@ instance:

::

with File('demo.txt', 'wb') as opened_file:
opened_file.fuck('Hola!')
with File('demo.txt', 'w') as opened_file:
opened_file.undefined_function('Hola!')

Let's list down the steps which are taken by the ``with`` statement when
an error is encountered.
Expand All @@ -111,7 +111,7 @@ statement is encountered then the method returns ``None``). Therefore,

Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: 'file' object has no attribute 'fuck'
AttributeError: 'file' object has no attribute 'undefined_function'

Let's try handling the exception in the ``__exit__`` method:

Expand All @@ -127,8 +127,8 @@ Let's try handling the exception in the ``__exit__`` method:
self.file_obj.close()
return True

with File('demo.txt', 'wb') as opened_file:
opened_file.fuck()
with File('demo.txt', 'w') as opened_file:
opened_file.undefined_function()

# Output: Exception has been handled

Expand All @@ -152,7 +152,7 @@ Let's see a basic, useless example:

@contextmanager
def open_file(name):
f = open(name, 'wb')
f = open(name, 'w')
yield f
f.close()

Expand Down

0 comments on commit fe4b2b8

Please sign in to comment.