News for hackers. Stuff that matters.
Re:Facts and myths about Python names and values by njharman on Sunday July 14, 2013 @02:30PM (#6041668)
I'm not sure about list += one. Would have to dissemble to determine "why". The '+' operator is not defined for all possible sequence types (could never be given user definable types). So, in many instances (e.g. adding tuple to list) l=l+s will TypeError. += for list must be defined to "cast" the sequence into list. Something like "l = l + list(s)". List constructor must iterate over the sequence to create list. By definition a sequence must be iterable. So l += s will work for all (s).

"is" operator determines if two things are the same thing, contrast with two things evaluate to the same value, "==". It's probably implementation dependent but "same thing" is same memory location. CPython "interns" (keeps a table/cache) of small integers rather than allocating them anew (for speed and space efficiency). All names referring to the value of integer '4' are referring to the same value. Possible cause integers are immutable. Were as names referring to larger ints may or may not be referring to the same value, even when those values are equal.

Default mutable arguments have been talked about a lot. Google it. You should already grok it after learning what OA taught you about names / reference and knowning that function/method definitions are executed once (at import/"parse"). So, the list that the name "mutable" refers to is created once, when defined (which is usually once But inner functions and programatically generated functions could be more). All calls of that function will be using the same name "mutable" all of which refer to the same list.

  def func(mutable=[]): 
     mutable.append(len(mutable))
     print mutable
I'm not sure what is meant by class attribute and esp, given that he just wrote whole article on how names (attributes are names) don't have types, values do, what is meant by list or int attribute.

There are no comments on this comment yet.