Skip to content

Commit

Permalink
Merge pull request #10 from phihag/patch-10
Browse files Browse the repository at this point in the history
decorators: PEP8 and print as a function
  • Loading branch information
yasoob committed Aug 17, 2015
2 parents 87bd969 + 741f2b9 commit 55f8857
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions decorators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ First of all let's understand functions in python:
::

def hi(name="yasoob"):
return "hi "+name
return "hi " + name

print hi()
#output: 'hi yasoob'
print(hi())
# output: 'hi yasoob'

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

print greet()
#output: 'hi yasoob'
# output: 'hi yasoob'

#lets see what happens if we delete the old hi function!
# Let's see what happens if we delete the old hi function!
del hi
print hi()
print(hi())
#outputs: NameError

print greet()
print(greet())
#outputs: 'hi yasoob'

Defining functions within functions:
Expand All @@ -59,9 +59,9 @@ other functions:
def welcome():
return "now you are in the welcome() function"

print greet()
print welcome()
print "now you are back in the hi() function"
print(greet())
print(welcome())
print("now you are back in the hi() function")

hi()
#output:now you are inside the hi() function
Expand Down Expand Up @@ -101,13 +101,13 @@ can return it as an output as well:
return welcome

a = hi()
print a
print(a)
#outputs: <function greet at 0x7f2143c01500>

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

print a()
print(a())
#outputs: now you are in the greet() function

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

def doSomethingBeforeHi(func):
print "I am doing some boring work before executing hi()"
print func()
print("I am doing some boring work before executing hi()")
print(func())

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

a_func()

print "I am doing some boring work after executing a_func()"
print("I am doing some boring work after executing a_func()")

return wrapTheFunction

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

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

a_function_requiring_decoration()
#outputs: I am doing some boring work before executing a_function_requiring_decoration()
Expand Down Expand Up @@ -219,16 +219,16 @@ that is ``functools.wraps``. Let's modify our previous example to use
def a_new_decorator(a_func):
@wraps(a_func)
def wrapTheFunction():
print "I am doing some boring work before executing a_func()"
print("I am doing some boring work before executing a_func()")
a_func()
print "I am doing some boring work after executing a_func()"
print("I am doing some boring work after executing a_func()")
return wrapTheFunction

@a_new_decorator
def a_function_requiring_decoration():
"""Hey yo! Decorate me!"""
print "I am the function which needs some decoration to \
remove my foul smell"
print("I am the function which needs some decoration to "
"remove my foul smell")

print(a_function_requiring_decoration.__name__)
# Output: a_function_requiring_decoration
Expand All @@ -251,7 +251,7 @@ decorators.
@decorator_name
def func():
print "Function is running"
print("Function is running")
can_run = True
print(func())
Expand Down Expand Up @@ -307,7 +307,7 @@ Logging is another area where the decorators shine. Here is an example:
def logit(func):
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
print(func.__name__ + " was called")
return func(*args, **kwargs)
return with_logging
Expand Down

0 comments on commit 55f8857

Please sign in to comment.