CSC/ECE 517 Spring 2014/ch1 1w1l m: Difference between revisions
Jump to navigation
Jump to search
(→What is a closure?: A bit more trimming of closure example) |
(Added a design pattern example - decorators) |
||
Line 2: | Line 2: | ||
== Background == | == Background == | ||
=== | === Explanation of Closures === | ||
Very simply, a closure is a function that can use a variable that was valid within the scope that the closure was defined, but need not be in-scope where the closure is called. A quick example is very illustrative. | Very simply, a closure is a function that can use a variable that was valid within the scope that the closure was defined, but need not be in-scope where the closure is called. A quick example is very illustrative. | ||
Line 16: | Line 16: | ||
default_closure = closure_builder() | default_closure = closure_builder() | ||
custom_closure = closure_builder("Custom") | custom_closure = closure_builder("Custom") | ||
del closure_builder | |||
# Call the closures you built | # Call the closures you built | ||
Line 23: | Line 24: | ||
== Examples == | == Examples == | ||
=== Decorators === | |||
Decorators are an interesting and powerful language feature that can be implemented elegantly with closures. | |||
<code> | |||
#!/usr/bin/env python | |||
def decorate(func): | |||
def decorated_func(): | |||
print "About to call func" | |||
func() | |||
print "Back from calling func" | |||
return decorated_func | |||
@decorate | |||
def func_to_decorate(): | |||
print "In func_to_decorate" | |||
func_to_decorate() | |||
</code> | |||
<code> | |||
[~517/wiki]$ chmod ug+x decorator.py | |||
[~517/wiki]$ ./decorator.py | |||
About to call func | |||
In func_to_decorate | |||
Back from calling func | |||
</code> | |||
== Narration == | == Narration == |
Revision as of 07:27, 23 February 2014
Design Patterns Involving Closures
Background
Explanation of Closures
Very simply, a closure is a function that can use a variable that was valid within the scope that the closure was defined, but need not be in-scope where the closure is called. A quick example is very illustrative.
def closure_builder(message="Default"):
def closure():
# Message is in-scope here
print message
return closure
# Build two functions
default_closure = closure_builder()
custom_closure = closure_builder("Custom")
del closure_builder
# Call the closures you built
default_closure() # Amazingly, prints "Default"
custom_closure() # Amazingly, prints "Custom"
Examples
Decorators
Decorators are an interesting and powerful language feature that can be implemented elegantly with closures.
#!/usr/bin/env python
def decorate(func):
def decorated_func():
print "About to call func"
func()
print "Back from calling func"
return decorated_func
@decorate
def func_to_decorate():
print "In func_to_decorate"
func_to_decorate()
[~517/wiki]$ chmod ug+x decorator.py
[~517/wiki]$ ./decorator.py
About to call func
In func_to_decorate
Back from calling func
Narration
Links to Important Terms
References
<references />