Python quiz

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.)


Posted

in

by

Tags:

Comments

2 responses to “Python quiz”

  1. leg Avatar
    leg

    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?

  2. Gregory Nicholas Avatar

    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

Your email address will not be published. Required fields are marked *