Advertisement
Guest User

Untitled

a guest
Jan 31st, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. # Time to make a function that modifies its own behaviour after the first time
  2. # it's called with specific arguments!
  3.  
  4. needs_input = True
  5. def canvas_read(x, y):
  6.     global needs_input
  7.     # Only ask for input the first time x>=0, y=-1 is read
  8.     if needs_input and y == -1 and x >= 0:
  9.         get_input()
  10.         needs_input = False
  11.    
  12.     if (x, y) not in canvas:
  13.         canvas[(x,y)] = (0, 0)
  14.     return canvas[(x,y)]
  15.  
  16. # Hmm... Global variables are gross. Let's try that again.
  17.  
  18. def canvas_read(x, y, IGNORE_ME=[1]):
  19.     # Only ask for input the first time x>=0, y=-1 is read
  20.     if IGNORE_ME[-1] and y == -1 and x >= 0:
  21.         get_input()
  22.         IGNORE_ME.append(0)
  23.    
  24.     if (x, y) not in canvas:
  25.         canvas[(x,y)] = (0, 0)
  26.     return canvas[(x,y)]
  27.  
  28. # Abusing mutable default argument behaviour? If Python ever changes it to be
  29. # less silly, this will be hard to port to Python 3. One more try.
  30.  
  31. def canvas_read(x, y):
  32.     # Only ask for input the first time x>=0, y=-1 is read
  33.     if y == -1 and x >= 0:
  34.         get_input()
  35.        
  36.         def no_input_check(x,y):
  37.             if (x, y) not in canvas:
  38.                 canvas[(x,y)] = (0, 0)
  39.             return canvas[(x,y)]
  40.        
  41.         canvas_read.__code__ = no_input_check.__code__
  42.    
  43.     if (x, y) not in canvas:
  44.         canvas[(x,y)] = (0, 0)
  45.     return canvas[(x,y)]
  46.  
  47. # This is the most beautiful code I've ever written.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement