I was reading source code of collections.py
yesterday.
In the namedtuple
function, a template
string is generated and then exec
ed in a temporary namespace.
In the namespace dict, property
is renamed to _property
and tuple
to _tuple
.
I wonder what's the reason behind this. What problems does this renaming helps avoid?
In general, the underscore names are sometimes used in the standard library to to keep a namespace clean.
In the case of _tuple, it was necessary because you're allowed to use "tuple" as a field name:
>>> Example = namedtuple('Example', ['list', 'tuple', 'property'])
>>> e.list
[10, 20, 30]
>>> e.tuple
(40, 50, 60)
>>> e.property
'Boardwalk'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With