Python: 'Private' module in a package

前端 未结 5 775
小蘑菇
小蘑菇 2020-12-05 06:23

I have a package mypack with modules mod_a and mod_b in it. I intend the the package itself and mod_a to be imported free

5条回答
  •  攒了一身酷
    2020-12-05 06:51

    Python doesn't strictly know or support "private" or "protected" methods or classes. There's a convention that methods prefixed with a single underscore aren't part of an official API, but I wouldn't do this on classes or files - it's ugly.

    If someone really needs to subclass or access mod_b, why prevent him/her from doing so? You can always supply a preferred API in your documentation and document in your module that you shouldn't access it directly and use mypack in stead.

提交回复
热议问题