Hacker News new | past | comments | ask | show | jobs | submit login

haha, your explanation is exactly the mutable vs immutable thing.

dicts are mutable, so, it doesn't create a new object to make an assignment

numbers are immutable, so, it does.




Except that's 100% wrong here. There's not even a facility for marking an object as immutable in Python, so that wouldn't support user-defined classes at all.

Python is always pass-by-object-reference, and assignment is always a pointer operation. It's not special-cased like you're describing.


Indeed, this may or may not be clearer with the help of id() - observe:

  >>> v=2
  >>> id(v)
  4148001784
  >>> id(2)
  4148001784
  >>> def m(a):
  ...   a=3
  ...
  >>> m(v)
  >>> id(3)
  4148001800
  >>> id(v)
  4148001784
  >>> v
  2


  >>> l=[]
  >>> id(l)
  4140973392
  >>> def m2(lst):
  ...   lst.append(1)
  ...
  >>> m2(l)
  >>> l
  [1]
  >>> id(l)
  4140973392




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: