News for hackers. Stuff that matters.
Re:Redesigning Python's named tuples by danpalmer on Friday September 01, 2017 @05:15AM (#15147112)
> I mean really, when was the last time you ever accessed a namedtuple by index?

Yesterday. I use this quite a bit. I like non-named tuples as a data structure and use them regularly, and often partially upgrade a tuple to a named tuple.

Changing these sorts of things is what caused the Python 2/3 split, I don't think we as a community want that again for a while, so backwards compatibility for things like this that aren't _that_ uncommon should be a priority.

Re:Redesigning Python's named tuples by hyperbovine on Friday September 01, 2017 @08:21AM (#15147728)
Agreed, I use the fact that "namedtuples are tuples" constantly. For one thing, you can pass them to 3rd party libraries that take tuples while maintaining readability on your end. I would be horrified if they dropped index access. It would break everything.

I should say though, for simple POJO-style classes @attr.s (https://github.com/python-attrs/attrs) has replaced 90% of my namedtuple usage.

Re:Redesigning Python's named tuples by pmiller2 on Friday September 01, 2017 @05:29AM (#15147159)
"Partially upgrade a tuple"? What does that mean?
Re:Redesigning Python's named tuples by 4c2383f5c88e911 on Friday September 01, 2017 @05:37AM (#15147183)
It means that if you have a data structure that is represented by a tuple, you can switch the creation call with a namedtuple of the same size, keeping the tuple API, therefore not breaking any older code that may rely on it, while at the same time providing a cleaner interface (using the attribute names) to newer code.

Of course, old code should be upgraded to use attribute names, but it's really convenient to be able to upgrade it painlessy.

Re:Redesigning Python's named tuples by eesmith on Friday September 01, 2017 @07:07AM (#15147449)
As a concrete example, in Python 1.5.2, urlparse.py:urlparse returned a 6-element tuple:

  # Parse a URL into 6 components:
  # <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
  # Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
  # Note that we don't break the components up in smaller bits
  # (e.g. netloc is a single string) and we don't expand % escapes.
  def urlparse(url, scheme = '', allow_fragments = 1):
      ...
      tuple = scheme, netloc, url, params, query, fragment
      _parse_cache[key] = tuple
      return tuple
This API was around for years, with people using tuple indexing either explicitly or through destructuring assignment.

This was replaced with a namedtuple. The following is from Python 2.7.10:

  class ParseResult(namedtuple('ParseResult', 'scheme netloc path params query fragment'), ResultMixin):

    __slots__ = ()

    def geturl(self):
        return urlunparse(self)


  def urlparse(url, scheme='', allow_fragments=True):
    """Parse a URL into 6 components:
    <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
    Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
    Note that we don't break the components up in smaller bits
    (e.g. netloc is a single string) and we don't expand % escapes."""
    tuple = urlsplit(url, scheme, allow_fragments)
    scheme, netloc, url, query, fragment = tuple
    if scheme in uses_params and ';' in url:
        url, params = _splitparams(url)
    else:
        params = ''
    return ParseResult(scheme, netloc, url, params, query, fragment)
The old tuple-based API still works, so it doesn't break backwards compatibility, while it also gives people the ability to migrate to an attribute-based API.

That said, it's very hard to remove the old API. The latest version of Python, in url/parse.py:urlparse, still supports tuple-based indexing.

Perhaps Python 4 will remove that behavior? Probably not. At the very least there would need to be a namedtuplemigration type which gives a warning if the old tuple API is used when only attribute lookup is expected.

Re:Redesigning Python's named tuples by masklinn on Friday September 01, 2017 @09:38AM (#15148206)
> This API was around for years, with people using tuple indexing either explicitly or through destructuring assignment.

FWIW destructuring assignment uses the iteration protocol[0] not indexing.

[0] which can fallback on indexing if there is no explicit iteration support, IME that tends to be more unhelpful than convenient

Re:Redesigning Python's named tuples by eesmith on Friday September 01, 2017 @10:29AM (#15148572)
Hmm, I think meant to say something like "using either explicit tuple indexing or through destructuring assignment". Words, mixed up, they were. But yes, what I wrote was wrong.
Re:Redesigning Python's named tuples by danpalmer on Friday September 01, 2017 @05:10PM (#15152403)
Thanks for the great answer!