News for hackers. Stuff that matters.
Re:Python's Mutable Default Problem by dustingram on Sunday February 20, 2011 @05:39PM (#2243373)
This 'problem' can actually come in handy when used with a regex callback function.

See if you can determine what this does:

    def cbk(match, nb = [0] ):
        if len(match.group())==len(nb):
            nb[-1] += 1
        elif  len(match.group())>len(nb):
            nb.append(1)
        else:
            nb[:] = nb[0:len(match.group())]
            nb[-1] += 1
        return match.group()+' '+('.'.join(map(str,nb)))
    
    str = re.compile('^(#+)',re.MULTILINE).sub(cbk,str)
Re:Python's Mutable Default Problem by d0mine on Sunday February 20, 2011 @09:00PM (#2243912)
The code converts:

  ##
  #
To:

  ## 0.1
  # 1
str is builtin, don't use it as a variable name especially if you use it in its original role.
Re:Python's Mutable Default Problem by dustingram on Monday February 21, 2011 @10:44AM (#2245766)
Correct, & thanks for the tip! But don't worry, I changed the variable name from my copy & paste and just didn't give it a second thought.