lowatt
lowatt

Reputation: 363

python cannot call module method from staticmethod?

consider the following pattern:

'''some module body'''

def __foo():
    '''module method foo'''
    pass

class Dummy(object):
    @staticmethod
    def bar():
        __foo()

__foo() # No Error.
Dummy.bar() #NameError: Global name "_Dummy__foo" is not defined.

Why is this happening?

--

And if it's bad to name it with "__", what is the best practice in Python to make module methods available only for inner-module functions/methods?

Upvotes: 4

Views: 153

Answers (1)

user2357112
user2357112

Reputation: 280237

Don't start names with double underscores. Any identifier found in a class statement, starting with at least 2 underscores and ending with less than 2 underscores, gets _Classname prepended to it, where Classname is the name of the class. This is supposed to provide limited support for private-ish variables, but using it is often considered bad practice.

Upvotes: 5

Related Questions