0

I was recently looking to practice utilizing dictionaries in python and stumbled across these two pieces of information. Both pieces of code use empty dictionaries and (to me) are the same thing. I suppose I'm asking for the difference between these two and which one is more advantageous and/or more convenient.

#1

    def foo(dct = {}):

#2

    def foo(dct = None):
        if dct == None:
            dct = {}
j0k
  • 22,600
  • 28
  • 79
  • 90
Raj
  • 81
  • 1
  • 8
  • possible duplicate of ["Least Astonishment" in Python: The Mutable Default Argument](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) – jdi Jun 28 '12 at 16:25

1 Answers1

1

Don't use a mutable object as the default argument to a function unless you really know what you're doing; every time the function is invoked it will use the same dictionary, since it's created when the function is defined, not when it's invoked.

Wooble
  • 87,717
  • 12
  • 108
  • 131