0

I've been told I should use;

def some_method(self,a,b=[]):
    etc...

instead of

def some_method(self,a,b=list()):
    etc...

'just because' .. well, I want to know 'why'!

As far as I am aware, there is no real difference between the two?

Matt Warren
  • 669
  • 8
  • 18
  • 5
    You should definitely not use either. Whoever told you to is simply wrong. – Daniel Roseman Aug 28 '13 at 13:10
  • 2
    Neither of those will do what you probably want... – Chris Pfohl Aug 28 '13 at 13:10
  • 2
    This is not an exact duplicate... However, neither should be used and the post: http://stackoverflow.com/questions/18488858/how-to-properly-declare-a-an-empty-list-as-a-default-parameter explains the correct way of doing this... - Also related (and it's duplicates): [mylist = list() vs mylist = \[\] in Python](http://stackoverflow.com/questions/11780357/mylist-list-vs-mylist-in-python) – Jon Clements Aug 28 '13 at 13:11
  • It's generally only ok if you are not mutating the list, eg if you are iterating over it. I've never seen the second form, although some people prefer more "pronounceable" code. – John La Rooy Aug 28 '13 at 13:25

0 Answers0