Reputation: 33
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
Upvotes: 1
Views: 66
Reputation: 281012
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.
Upvotes: 1
Reputation: 26900
You MAY access like so:
SlackApi._SlackApi__Team._Team__Profile
or like so:
self._Team__Profile
But that's just wrong. For your own convenience, don't make them a private class.
Upvotes: 0