10
3
Write a few lines of Python code, X
, that does not reference any global variables, such that
def method():
X
print(a)
method()
prints 1
but
def method():
X
X
print(a)
method()
prints 2
.
So, I hate to be a stickler, but it seems like vars
and locals
are actually global variables in Python:
def test_global_1():
global vars, locals
vars = lambda: 2
locals = lambda: 3
def test_global_2():
print(vars())
print(locals())
test_global_1()
test_global_2()
Also, it looks like people would like to see objective winning criteria for puzzles like this. Code length doesn't really feel right here so perhaps we could make a system of brownie points for various novel features of the code? I'm not sure exactly what these could be but here's a start:
- +1 for really truly no globals (no
vars
orlocals
) - +1 for being the first to post a particular technique
- +1 for the shortest solution posted
- +1 for a solution involving only a single Python statement
- +1 for interesting "hacks" like joining at lexical non-boundaries
- +1 for not using exceptions
And if you can think of more you could edit this question to add to the list.
Can this problem be solved without using exceptions and without using globals like vars
and locals
? I suspect it can, though I haven't figured out exactly how yet...
Good puzzle! I made sure not to scroll down so I could solve it myself without seeing anyone's answers. :D – mbomb007 – 2015-07-30T21:36:50.193
1Thanks for the puzzles Owen, and welcome to the site. There is a rule on the site that all questions must have an objective winning condition, so you should probably add one. One possibility is shortest length of
X
, but there are other options. – isaacg – 2015-07-31T08:25:53.4173"all questions must have an objective winning condition" - Stupid rule imho. Who cares about a "winner" when we all actually most enjoy the puzzling and learning from the different answers. – JimmyB – 2015-07-31T11:34:43.923
2Please add a [tag:code-golf] or [tag:popularity-contest] tag, depending on whether you want people to optimize for shortness code or for general popularity. I imagine code-golf is better for this challenge (popularity-contest is encouraged only for challenges that cannot be easily classified otherwise), but it's up to you. – apsillers – 2015-07-31T13:18:52.373
Ongoing meta discussion. – xnor – 2015-07-31T22:52:15.540
2You've added a scoring system, but also added the popularity contest tag, which means that a winner is decided by votes. What do you mean here? Perhaps you want votes just as a tiebreak? – xnor – 2015-08-02T02:13:39.300
You've added something saying 'vars' and 'locals' are in fact global variables, but I think your reasoning is flawed. Prior to your defining 'vars' and 'locals', they weren't global variables; this can be verified by looking at globals(). – Don Hatch – 2015-08-03T23:08:20.177
The restrictions you're adding are starting to make me think an answer is provably impossible. And I think your wording "a few lines of code" implies complete lines, which rules out clever hacks like joining at non-lexical boundaries. So, could I win by providing a compelling impossibility proof? – Don Hatch – 2015-08-04T02:00:31.213
@DonHatch An impossibility proof would count, yes. – Owen – 2015-08-04T02:02:59.797
Unfortunately question is on hold but I want to sneak in my answer anyway :P My code
X
would bea=method.a=method.__dict__.get('a',0)+1
. – swenzel – 2015-08-04T20:17:09.383No fun allowed guys – Robert Fraser – 2016-08-14T20:01:28.207