Skip to content

Commit 741f2b9

Browse files
committed
decorators: PEP8 and print as a function
Be more consistent about prints
1 parent 848fabe commit 741f2b9

1 file changed

Lines changed: 27 additions & 27 deletions

File tree

decorators.rst

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ First of all let's understand functions in python:
2020
::
2121

2222
def hi(name="yasoob"):
23-
return "hi "+name
23+
return "hi " + name
2424

25-
print hi()
26-
#output: 'hi yasoob'
25+
print(hi())
26+
# output: 'hi yasoob'
2727

28-
#We can even assign a function to a variable like
28+
# We can even assign a function to a variable like
2929
greet = hi
30-
#We are not using parentheses here because we are not calling the function hi
31-
#instead we are just putting it into the greet variable. Let's try to run this
30+
# We are not using parentheses here because we are not calling the function hi
31+
# instead we are just putting it into the greet variable. Let's try to run this
3232

3333
print greet()
34-
#output: 'hi yasoob'
34+
# output: 'hi yasoob'
3535

36-
#lets see what happens if we delete the old hi function!
36+
# Let's see what happens if we delete the old hi function!
3737
del hi
38-
print hi()
38+
print(hi())
3939
#outputs: NameError
4040

41-
print greet()
41+
print(greet())
4242
#outputs: 'hi yasoob'
4343

4444
Defining functions within functions:
@@ -59,9 +59,9 @@ other functions:
5959
def welcome():
6060
return "now you are in the welcome() function"
6161

62-
print greet()
63-
print welcome()
64-
print "now you are back in the hi() function"
62+
print(greet())
63+
print(welcome())
64+
print("now you are back in the hi() function")
6565

6666
hi()
6767
#output:now you are inside the hi() function
@@ -101,13 +101,13 @@ can return it as an output as well:
101101
return welcome
102102

103103
a = hi()
104-
print a
104+
print(a)
105105
#outputs: <function greet at 0x7f2143c01500>
106106

107107
#This clearly shows that `a` now points to the greet() function in hi()
108108
#Now try this
109109

110-
print a()
110+
print(a())
111111
#outputs: now you are in the greet() function
112112

113113
Just take a look at the code again. In the ``if/else`` clause we are
@@ -131,8 +131,8 @@ Giving a function as an argument to another function:
131131
return "hi yasoob!"
132132

133133
def doSomethingBeforeHi(func):
134-
print "I am doing some boring work before executing hi()"
135-
print func()
134+
print("I am doing some boring work before executing hi()")
135+
print(func())
136136

137137
doSomethingBeforeHi(hi)
138138
#outputs:I am doing some boring work before executing hi()
@@ -156,12 +156,12 @@ previous decorator and make a little bit more usable program:
156156

157157
a_func()
158158

159-
print "I am doing some boring work after executing a_func()"
159+
print("I am doing some boring work after executing a_func()")
160160

161161
return wrapTheFunction
162162

163163
def a_function_requiring_decoration():
164-
print "I am the function which needs some decoration to remove my foul smell"
164+
print("I am the function which needs some decoration to remove my foul smell")
165165

166166
a_function_requiring_decoration()
167167
#outputs: "I am the function which needs some decoration to remove my foul smell"
@@ -186,8 +186,8 @@ run the previous code sample using @.
186186
@a_new_decorator
187187
def a_function_requiring_decoration():
188188
"""Hey yo! Decorate me!"""
189-
print "I am the function which needs some decoration to \
190-
remove my foul smell"
189+
print("I am the function which needs some decoration to "
190+
" remove my foul smell")
191191

192192
a_function_requiring_decoration()
193193
#outputs: I am doing some boring work before executing a_function_requiring_decoration()
@@ -219,16 +219,16 @@ that is ``functools.wraps``. Let's modify our previous example to use
219219
def a_new_decorator(a_func):
220220
@wraps(a_func)
221221
def wrapTheFunction():
222-
print "I am doing some boring work before executing a_func()"
222+
print("I am doing some boring work before executing a_func()")
223223
a_func()
224-
print "I am doing some boring work after executing a_func()"
224+
print("I am doing some boring work after executing a_func()")
225225
return wrapTheFunction
226226

227227
@a_new_decorator
228228
def a_function_requiring_decoration():
229229
"""Hey yo! Decorate me!"""
230-
print "I am the function which needs some decoration to \
231-
remove my foul smell"
230+
print("I am the function which needs some decoration to "
231+
"remove my foul smell")
232232

233233
print(a_function_requiring_decoration.__name__)
234234
# Output: a_function_requiring_decoration
@@ -251,7 +251,7 @@ decorators.
251251
252252
@decorator_name
253253
def func():
254-
print "Function is running"
254+
print("Function is running")
255255
256256
can_run = True
257257
print(func())
@@ -307,7 +307,7 @@ Logging is another area where the decorators shine. Here is an example:
307307
def logit(func):
308308
@wraps(func)
309309
def with_logging(*args, **kwargs):
310-
print func.__name__ + " was called"
310+
print(func.__name__ + " was called")
311311
return func(*args, **kwargs)
312312
return with_logging
313313

0 commit comments

Comments
 (0)