I'm trying to make an instance of a __Profile
class in the constructor of the __Team
class, but I can't get access to __Profile
. How should I do it?
This is my code
class SlackApi:
# my work
class __Team:
class __Profile:
def get(self):
# my work
def __init__(self, slackApi):
self.slackApi = slackApi
self.profile = __Profile()
self.profile = __Team.__Profile()
self.profile = SlackApi.__Team.__Profile()
# I tried to all of these case, but I failed
# I need to keep '__Team', '__Profile' class as private class
My python version is 3.5.1
Python does not have access modifiers. If you try to treat __
like a traditional private
access modifier, this is one of the problems you get. Leading double underscores cause name mangling - a name __bar
inside a class Foo
(or class __Foo
, or any number of leading underscores before Foo
) will be mangled to _Foo__bar
.
If you really want to keep those leading double underscores, you'll have to explicitly mangle the names yourself:
self.profile = SlackApi._SlackApi__Team._Team__Profile()
This is also how you would access the __Profile
class from outside of SlackApi
altogether, so you're basically bypassing any pretense that these things are private.