Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Let's assume we have a function that return current time, if we have the time as default value it will always show the same results:
- import time
- def show_time(current_time = time.ctime()):
- print(current_time)
- # by calling the function show_time() withput arguments it will alwsays show same results
- show_time()
- #Sat Mar 16 12:26:42 2019
- show_time()
- #Sat Mar 16 12:26:42 2019
- ..
- # again, this is happening because the default arguments evaluted only once at the time of creating the function.
- #one solution is to call the function ctime() inside the function, and that way you evaluate on the function call.
- def show_time(current_time = time.ctime):
- print(current_time()) #notice we are calling the time function
- show_time()
- #Sat Mar 16 12:27:30 2019
- show_time()
- #Sat Mar 16 12:27:31 2019
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement