Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How do I disallow imports of a class from a module?

I tried:

__all__ = ['SpamPublicClass']

But, of course that's just for:

from spammodule import *

Is there a way to block importing of a class. I'm worried about confusion on the API level of my code that somebody will write:

from spammodule import SimilarSpamClass

and it'll cause debugging mayhem.

like image 671
orokusaki Avatar asked Feb 01 '10 15:02

orokusaki


1 Answers

The convention is to use a _ as a prefix:

class PublicClass(object):
    pass

class _PrivateClass(object):
    pass

The following:

from module import *

Will not import the _PrivateClass.

But this will not prevent them from importing it. They could still import it explicitly.

from module import _PrivateClass
like image 116
Nadia Alramli Avatar answered Nov 15 '22 19:11

Nadia Alramli