Python name mangling

后端 未结 11 2278
天命终不由人
天命终不由人 2020-11-22 05:31

In other languages, a general guideline that helps produce better code is always make everything as hidden as possible. If in doubt about whether a variable should be privat

11条回答
  •  Happy的楠姐
    2020-11-22 05:57

    "If in doubt about whether a variable should be private or protected, it's better to go with private." - yes, same holds in Python.

    Some answers here say about 'conventions', but don't give the links to those conventions. The authoritative guide for Python, PEP 8 states explicitly:

    If in doubt, choose non-public; it's easier to make it public later than to make a public attribute non-public.

    The distinction between public and private, and name mangling in Python have been considered in other answers. From the same link,

    We don't use the term "private" here, since no attribute is really private in Python (without a generally unnecessary amount of work).

提交回复
热议问题