k9x k9x - 1 year ago 68
Python Question

Is there a use for _tuple in Python?

I read the official documentation of

today and found
in the
method. I did not find where the
was defined.

You can try running the code below in Python, it does not raise any error.

>>> Point = namedtuple('Point', ['x', 'y'], verbose=True)
class Point(tuple):
'Point(x, y)'

__slots__ = ()

_fields = ('x', 'y')

def __new__(_cls, x, y):
'Create a new instance of Point(x, y)'
return _tuple.__new__(_cls, (x, y)) # Here. Why _tuple?

Update: What are the advantages of

from builtins import property as _property, tuple as _tuple

Is that just to let
be a protected value? Am I right?

Answer Source

From the generic source code (you can see the source code generated for this specific namedtuple by printing Point._source):

from builtins import property as _property, tuple as _tuple

So _tuple here is just an alias for built-in tuple:

In [1]: from builtins import tuple as _tuple

In [2]: tuple is _tuple
Out[2]: True

Namedtuple appeared in Python 2.6. The initial source for its __new__ method was

def __new__(cls, %(argtxt)s):
    return tuple.__new__(cls, (%(argtxt)s)) \n

The thing is, the source code is in string. They later format it using % locals(). If the tuple was listed in argtxt, the tuple.__new__ would have called __new__ method on whatever the tuple field contained. _tuple works as expected because namedtuple doesn't allow field names starting with _.

The bug was fixed in Python 2.6.3 release (see the changelog - collections.namedtuple() was not working with the following field names: cls, self, tuple, itemgetter, and property.).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download