1

This code:

import matplotlib.pyplot as plt
fig, axes = plt.subplots(12,2, figsize = (12,40), squeeze=False)
print(type(axes[0,0]))

Gives the following output:

<class 'matplotlib.axes._subplots.AxesSubplot'>

Why is there a leading underscore in the module name "_subplots"?

This is not a duplicate of the following question since my question refers to module naming only and not other cases. What is the meaning of a single and a double underscore before an object name?

Henrik Leijon
  • 1,277
  • 2
  • 10
  • 15
  • Possible duplicate of [What is the meaning of a single and a double underscore before an object name?](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name) – Nils Aug 16 '19 at 09:19
  • It is not a duplicate since my question refers to module naming only and not other cases. – Henrik Leijon Aug 16 '19 at 09:27

3 Answers3

1

As a general rule and if you follow the PEP8 standard, a single underscore in front of any name indicates that entity is not meant as an official API and is used internally. It remains visible though and Python treats it as any other class/variable/module so feel free to use it if you absolutely know what you are doing.

A double underscore is meant to "hide" something (making it somehow private). Python will treat this a bit differently and use name mangling so that is not immediately accessible. An attribute name __foo in class Bar will not be accessible in bar.__foo, as python will rename it at runtime to bar._Bar__foo.

EDIT

For modules specifically it serves the same purpose. A single "_" indicates all classes/functions/constants in the module are meant for "weak" internal use only. It does not mean you should never use them but it implies you have knowledge/understanding of the inner workings of the package.

Valentin B.
  • 602
  • 6
  • 18
0

It's because the person who wrote matplotlib, named it with an underscore.

It's not just matplotlib having that, that really means protected attribute.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
-1

Because the _subplots is a protected attribute of axes. "_" indicates protected attribute and "__" indicates the private attribute. You can read more about protected/private attributes in Python: https://www.tutorialsteacher.com/python/private-and-protected-access-modifiers-in-python

milanbalazs
  • 4,811
  • 4
  • 23
  • 45
  • Your answer is misleading because there is no such thing as protected and private attributes in Python. It is a naming convention to indicate this module should not be tempered with from the outside unless you absolutely know what you are doing. Double underscore indicates something that will definitely break the code if you touch it. – Valentin B. Aug 16 '19 at 09:21
  • Single underscore indicates the protected attributes but Python allows to use it outside of object but PEP8 warns you that it is a protected attribute and you should't use it but is allowed. And souble underscores indicate the private attributes and you cannot use them outside of object (These attributes are not visible outside of object). On the other hand, I agree that the Python handles the protected/private attributes than other OOP languages... Please see the link in my answer and you can get more details about Python protected and private attributes. – milanbalazs Aug 16 '19 at 09:25
  • "you should't use it but **is allowed**". You just proved my point. Python does absolutely nothing special with names starting with a "_", it's just a convention enforced by PEP8 to indicate that the class/attribute/module is needed for internal use only and should *probably* not be used outside of the package. – Valentin B. Aug 16 '19 at 09:29
  • I totally agree with you point! I think we say the same thing. So as I mentioned the single underscore has mean in Python, but it doesn't have functional effect to code. :) – milanbalazs Aug 16 '19 at 09:34
  • 1
    Yes I get that you get it :) it's just that coming from Java for example your answer can be a bit misleading because protected and private entities **are** treated differently by the language. – Valentin B. Aug 16 '19 at 09:38