Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Time to make a function that modifies its own behaviour after the first time
- # it's called with specific arguments!
- needs_input = True
- def canvas_read(x, y):
- global needs_input
- # Only ask for input the first time x>=0, y=-1 is read
- if needs_input and y == -1 and x >= 0:
- get_input()
- needs_input = False
- if (x, y) not in canvas:
- canvas[(x,y)] = (0, 0)
- return canvas[(x,y)]
- # Hmm... Global variables are gross. Let's try that again.
- def canvas_read(x, y, IGNORE_ME=[1]):
- # Only ask for input the first time x>=0, y=-1 is read
- if IGNORE_ME[-1] and y == -1 and x >= 0:
- get_input()
- IGNORE_ME.append(0)
- if (x, y) not in canvas:
- canvas[(x,y)] = (0, 0)
- return canvas[(x,y)]
- # Abusing mutable default argument behaviour? If Python ever changes it to be
- # less silly, this will be hard to port to Python 3. One more try.
- def canvas_read(x, y):
- # Only ask for input the first time x>=0, y=-1 is read
- if y == -1 and x >= 0:
- get_input()
- def no_input_check(x,y):
- if (x, y) not in canvas:
- canvas[(x,y)] = (0, 0)
- return canvas[(x,y)]
- canvas_read.__code__ = no_input_check.__code__
- if (x, y) not in canvas:
- canvas[(x,y)] = (0, 0)
- return canvas[(x,y)]
- # This is the most beautiful code I've ever written.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement