You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/session08.rst
+39-39
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,7 @@ Decorators
51
51
52
52
defsubstitute(a_function):
53
53
defnew_function(*args, **kwargs):
54
-
return"I'm not that other function"
54
+
returnu"I'm not that other function"
55
55
return new_function
56
56
57
57
A Definition
@@ -93,9 +93,9 @@ one:
93
93
.. code-block:: python
94
94
95
95
defadd(a, b):
96
-
print"Function 'add' called with args: %r"%locals()
96
+
print(u"Function 'add' called with args: %r"%locals())
97
97
result = a + b
98
-
print"\tResult --> %r"% result
98
+
print(u"\tResult --> %r"% result)
99
99
return result
100
100
101
101
.. nextslide::
@@ -109,13 +109,13 @@ Now imagine we defined the following, more generic *decorator*:
109
109
110
110
deflogged_func(func):
111
111
deflogged(*args, **kwargs):
112
-
print"Function %r called"% func.__name__
112
+
print(u"Function %r called"% func.__name__)
113
113
if args:
114
-
print"\twith args: %r"% args
114
+
print(u"\twith args: %r"% args)
115
115
if kwargs:
116
-
print"\twith kwargs: %r"% kwargs
116
+
print(u"\twith kwargs: %r"% kwargs)
117
117
result = func(*args, **kwargs)
118
-
print"\t Result --> %r"% result
118
+
print(u"\t Result --> %r"% result)
119
119
return result
120
120
return logged
121
121
@@ -291,7 +291,7 @@ Let's define another decorator that will time how long a given call takes:
291
291
start = time.time()
292
292
result = func(*args, **kwargs)
293
293
elapsed = time.time() - start
294
-
print"time expired: %s"% elapsed
294
+
print(u"time expired: %s"% elapsed)
295
295
return result
296
296
return timed
297
297
@@ -380,7 +380,7 @@ Remember this from last week?
380
380
defdelx(self):
381
381
delself._x
382
382
x =property(getx, setx, delx,
383
-
"I'm the 'x' property.")
383
+
u"I'm the 'x' property.")
384
384
385
385
.. nextslide:: The Decorator version
386
386
@@ -430,14 +430,14 @@ The Iterator Protocol
430
430
An iterator must have the following methods:
431
431
432
432
.. code-block:: python
433
-
433
+
434
434
an_iterator.__iter__()
435
435
436
436
Returns the iterator object itself. This is required to allow both containers
437
437
and iterators to be used with the ``for`` and ``in`` statements.
438
438
439
439
.. code-block:: python
440
-
440
+
441
441
an_iterator.next()
442
442
443
443
Returns the next item from the container. If there are no further items,
@@ -502,10 +502,10 @@ The ``iter()`` function:
502
502
In [20]: iter([2,3,4])
503
503
Out[20]: <listiterator at 0x101e01350>
504
504
505
-
In [21]: iter("a string")
505
+
In [21]: iter(u"a string")
506
506
Out[21]: <iterator at 0x101e01090>
507
507
508
-
In [22]: iter( ('a', 'tuple') )
508
+
In [22]: iter( (u'a', u'tuple') )
509
509
Out[22]: <tupleiterator at 0x101e01710>
510
510
511
511
for an arbitrary object, ``iter()`` calls the ``__iter__`` method. But it knows about some object (``str``, for instance) that don't have a ``__iter__`` method.
@@ -568,14 +568,14 @@ In the ``Examples/Session08`` dir, you will find: ``iterator_1.py``
568
568
it = IterateMe_2(2, 20, 2)
569
569
for i in it:
570
570
if i >10: break
571
-
print i
571
+
print(i)
572
572
573
573
And then pick up again:
574
574
575
575
.. code-block:: python
576
576
577
577
for i in it:
578
-
print i
578
+
print(i)
579
579
580
580
* Does ``xrange()`` behave the same?
581
581
@@ -689,7 +689,7 @@ yet another way to make a generator:
689
689
>>> (x *2for x in [1, 2, 3])
690
690
<generator object<genexpr> at 0x10911bf50>
691
691
>>>for n in (x *2for x in [1, 2, 3]):
692
-
...print n
692
+
...print(n)
693
693
...246
694
694
695
695
@@ -761,7 +761,7 @@ Context Managers
761
761
762
762
.. code-block:: python
763
763
764
-
file_handle =open('filename.txt', 'r')
764
+
file_handle =open(u'filename.txt', u'r')
765
765
file_content = file_handle.read()
766
766
file_handle.close()
767
767
# do some stuff with the contents
@@ -781,7 +781,7 @@ You can write more robust code for handling your resources:
781
781
.. code-block:: python
782
782
783
783
try:
784
-
file_handle =open('filename.txt', 'r')
784
+
file_handle =open(u'filename.txt', u'r')
785
785
file_content = file_handle.read()
786
786
finally:
787
787
file_handle.close()
@@ -811,7 +811,7 @@ lines of defensive code have been replaced with this simple form:
0 commit comments