@@ -9,7 +9,7 @@ Context managers allow you to do specifically that. For example:
99
1010::
1111
12- with open('some_file', 'wb ') as opened_file:
12+ with open('some_file', 'w ') as opened_file:
1313 opened_file.write('Hola!')
1414
1515The above code opens the file, writes some data to it and then closes
@@ -18,7 +18,7 @@ close it. The above code is equivalent to:
1818
1919::
2020
21- file = open('some_file', 'wb ')
21+ file = open('some_file', 'w ')
2222 try:
2323 file.write('Hola!')
2424 finally:
@@ -57,7 +57,7 @@ a ``with`` statement. Let's try:
5757
5858::
5959
60- with File('demo.txt', 'wb ') as opened_file:
60+ with File('demo.txt', 'w ') as opened_file:
6161 opened_file.write('Hola!')
6262
6363Our ``__exit__ `` function accepts three arguments. They are required by
@@ -89,8 +89,8 @@ instance:
8989
9090::
9191
92- with File('demo.txt', 'wb ') as opened_file:
93- opened_file.fuck ('Hola!')
92+ with File('demo.txt', 'w ') as opened_file:
93+ opened_file.undefined_function ('Hola!')
9494
9595Let's list down the steps which are taken by the ``with `` statement when
9696an error is encountered.
@@ -111,7 +111,7 @@ statement is encountered then the method returns ``None``). Therefore,
111111
112112 Traceback (most recent call last):
113113 File "<stdin>", line 2, in <module>
114- AttributeError: 'file' object has no attribute 'fuck '
114+ AttributeError: 'file' object has no attribute 'undefined_function '
115115
116116Let's try handling the exception in the ``__exit__ `` method:
117117
@@ -127,8 +127,8 @@ Let's try handling the exception in the ``__exit__`` method:
127127 self.file_obj.close()
128128 return True
129129
130- with File('demo.txt', 'wb ') as opened_file:
131- opened_file.fuck ()
130+ with File('demo.txt', 'w ') as opened_file:
131+ opened_file.undefined_function ()
132132
133133 # Output: Exception has been handled
134134
@@ -152,7 +152,7 @@ Let's see a basic, useless example:
152152
153153 @contextmanager
154154 def open_file(name):
155- f = open(name, 'wb ')
155+ f = open(name, 'w ')
156156 yield f
157157 f.close()
158158
0 commit comments