Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can someone elaborate on this? I thought I had seen all the Python idioms but this one is new to me.


  def a(b=[]):
      b.append(1)
      print b
Now if you do,

  a()
  a()
you might expect to see

  [1]
  [1]
but you will actually see

  [1]
  [1, 1]
The list is not created at function call time, but at function definition time.


This has bitten me more times than I'd care to admit.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: