What will this code snippet print?
#!/usr/bin/python class A(object): def __init__(self, a={}): if not a.has_key('1'): a['1'] = 1 else: a['1'] += 1 print a['1'] for i in range(3): _ = A()
(Hint: it’s not not what you think.)
What will this code snippet print?
#!/usr/bin/python class A(object): def __init__(self, a={}): if not a.has_key('1'): a['1'] = 1 else: a['1'] += 1 print a['1'] for i in range(3): _ = A()
(Hint: it’s not not what you think.)
by
Tags:
So are you going to explain the model of python scoping that I should have in mind so I can think what the outcome is?
i know that you’re going to get screwed using :
def __init__(self, a={}):
that a arg is a going to get globally cached
Leave a Reply