[Tutor] Named tuple special methods and attributes start with an underscore?

boB Stepp robertvstepp at gmail.com
Tue Jun 2 22:04:38 EDT 2020


On Wed, Jun 03, 2020 at 11:23:04AM +1000, Cameron Simpson wrote:
>On 02Jun2020 18:12, boB Stepp <robertvstepp at gmail.com> wrote:

>>The mentioned 5 items are:  _make, _asdict, _replace, _fields and
>>_field_defaults.  Of the items "_replace" is the most surprising as that is
>>already used by Python in other contexts such as str.replace().  These
>>items don't look to be likely field names to be used by anyone.  Any
>>illumination for this design decision?  To my (perhaps naive) eyes this
>>strikes me as extraordinarily inconsistent Python syntax.
>
>By using leading underscores, the user of nametuple is free to use any 
>field names they like as long as they avoid underscores. Don't forget 
>that you can subclass a namedtuple, too. I've got several classes 
>which look like this:
>
>   class Something(namedtuple('Something', 'a b c')):
>       def some_method(...): ...
>
>You're saying I shouldn't be able to have a method called "replace" on 
>my arbitrary class?

Then why not str._replace() instead of the current str.replace()?  Is it
thought that users are less likely to want to modify the string class?  Is
this the design criteria -- the likeliness of user subclassing?

-- 
Wishing you only the best,

boB Stepp


More information about the Tutor mailing list