Created
January 13, 2017 22:03
-
-
Save leonchen417/376e98f74c1fc73c7684ff71a188e623 to your computer and use it in GitHub Desktop.
Closure,lambda,generator,decorator created by leonchen417 - https://repl.it/FIoG/31
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
#Scope and Namespaces | |
animal = 'friutbat' | |
def print_global(): | |
print('inside print_global:', animal) | |
def change_and_print_global(): #error will occur if called | |
print('inside print_global:', animal) | |
animal ='catfish' | |
print('after change', animal) | |
def change_local(): | |
animal = 'hotdog' | |
print('inside change_local:', animal,id(animal)) #id if this "animal" is different from global variable "animal" | |
# this is a inner function exmple | |
def knights(saying): | |
def inner(quote): | |
return "We are the knights who say: %s'" % quote | |
return inner(saying) | |
say = knights('RoHa') #returns a string | |
print(say) | |
# this is a Closure | |
def knights2(saying): | |
def inner2(): #saying is NOT pass into inner2 | |
return "We are the knights who say: %s'" % saying | |
return inner2 | |
say2 = knights2("Duck Duck") #say2 is a function | |
print(say2()) | |
#Anonymouse Function lambda() which means the function don't have name | |
def edit_story(words,funct): | |
for word in words: | |
print(funct(word)) | |
#usual function way | |
sound = ['roro','mai-law','fo-law','hiss'] | |
def enliven(word): | |
return word.capitalize() +'!' | |
print(edit_story(sound,enliven)) | |
# Anonymouse way | |
print(edit_story(sound, lambda word:word.capitalize()+ '!')) # lambda x, y: (x+y, x-y) show how to pass multiple args | |
#Generator - a self-define range function | |
def my_range(first=0,last=10,step=1): | |
number = first | |
while number < last: | |
yield number | |
number +=step | |
ranger = my_range(1,5) | |
print(type(ranger)) #this is a generator object | |
for x in ranger: | |
print(x) | |
for x2 in ranger: | |
print(x2, 'haha') #this shows nothing because it can only be iterate once, so this can't be re-count | |
#Decorator | |
def document_it(funct): | |
def new_function(*args,**kwargs): | |
print('Running function:' , funct.__name__) | |
print('Positional arguments:', args) | |
print('keyword arguments:',kwargs) | |
result = funct(*args,**kwargs) | |
print('Result:', result) | |
return result | |
return new_function | |
def square_it(funct): | |
def new_function2(*args,**kwargs): | |
print('Running function:' , funct.__name__) | |
result = funct(*args,**kwargs) | |
print('Result squared:', result*result) | |
return result*result | |
return new_function2 | |
def add_ints(a,b): | |
return a+b | |
rich_add_ints = document_it(add_ints) | |
print(rich_add_ints(3,8)) | |
@document_it | |
@square_it | |
def mul_ints(a,b): | |
return a*b | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment