Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasons to rename property to _property

I was reading source code of collections.py yesterday.

In the namedtuple function, a template string is generated and then execed 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?

like image 362
satoru Avatar asked Jul 07 '12 01:07

satoru


1 Answers

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'
like image 142
Raymond Hettinger Avatar answered Oct 16 '22 08:10

Raymond Hettinger