Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. # 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:
  2. import time
  3.  
  4. def show_time(current_time = time.ctime()):
  5. print(current_time)
  6.  
  7. # by calling the function show_time() withput arguments it will alwsays show same results
  8. show_time()
  9. #Sat Mar 16 12:26:42 2019
  10. show_time()
  11. #Sat Mar 16 12:26:42 2019
  12. ..
  13.  
  14. # again, this is happening because the default arguments evaluted only once at the time of creating the function.
  15.  
  16. #one solution is to call the function ctime() inside the function, and that way you evaluate on the function call.
  17. def show_time(current_time = time.ctime):
  18. print(current_time()) #notice we are calling the time function
  19.  
  20. show_time()
  21. #Sat Mar 16 12:27:30 2019
  22. show_time()
  23. #Sat Mar 16 12:27:31 2019
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement