mirror of
https://github.com/rasbt/python_reference.git
synced 2025-02-20 15:22:08 +00:00
closures
This commit is contained in:
parent
c7dd0dfdf7
commit
d28dd0b914
17
closures.py
Normal file
17
closures.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
# Python 3.x
|
||||
# sr 11/04/2013
|
||||
# closures
|
||||
#
|
||||
|
||||
def create_message(msg_txt):
|
||||
def _priv_msg(message): # private, no access from outside
|
||||
print("{}: {}".format(msg_txt, message))
|
||||
return _priv_msg # returns a function
|
||||
|
||||
new_msg = create_message("My message")
|
||||
# note, new_msg is a function
|
||||
|
||||
new_msg("Hello, World")
|
||||
# prints: "My message: Hello, World"
|
||||
|
||||
# print(dir(create_message.__closure__))
|
Loading…
Reference in New Issue
Block a user