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

``print a``? The inner `a` is the same reference as the outer `a`.

  a = 1;

  def foo(): b = a; return b

  foo() is a

  >> True
This gets hairy when referring to mutable types:

  a = []

  def foo(): b = a; return b

  foo().append(1)

  a

  >> [3]
Compare to immutable types:

  c = ()

  def bar(): d = c; return d

  bar() + (1,)

  c

  >> ()


I understand the context, but isn't it unfair to compare `append` and `+` ? Python is not clojure or sml, I think people assume mutability, and using foo() + [1] will consistently build fresh objects in both cases.


No, I don't think it's unfair because "fairness" has nothing to do with anything.

I was simply illustrating how using a mutable data structure defined in an outer scope can bite you.


Fair enough. May I mention the default parameter bite too. It's hard to notice at first.




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

Search: