Skip to content

Instantly share code, notes, and snippets.

@sidchilling
Created April 29, 2013 06:32
Show Gist options
  • Save sidchilling/5480010 to your computer and use it in GitHub Desktop.
Save sidchilling/5480010 to your computer and use it in GitHub Desktop.
Script to show how you can define a function inside another function
# 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