Created
April 29, 2013 06:32
-
-
Save sidchilling/5480010 to your computer and use it in GitHub Desktop.
Script to show how you can define a function inside another function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to show how to define functions inside functions and its scope | |
def talk(): | |
# We can define a function inside a function like this. In this case we define a | |
# function whisper() inside the funtion talk() | |
def whisper(word = 'yes'): | |
return '%s...' %(word) | |
# And we can use the function whisper right away inside the wrapping function | |
print whisper() | |
# We call the function talk(). The function whisper() is defined EVERY TIME the talk() | |
# function is called. | |
talk() | |
# Outputs: yes... | |
try: | |
print whisper(word = 'no') | |
except NameError as e: | |
print 'exception: %s' %(e) | |
# However, the "whisper" function DOES NOT EXIST outside the function talk() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment