Last active
October 30, 2018 23:03
-
-
Save AhmetCanSolak/3273f7df99b4e6f2af71b44ae145c9a5 to your computer and use it in GitHub Desktop.
A trial to implement non-accessible private methods in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pprint import pprint | |
# CamelCase because it "acts" like a class | |
def CounterController(): | |
class CounterControllerPrivate(object): | |
def __init__(self): | |
self.counter = 0 | |
def add_one(self): | |
self.counter += 1 | |
def reset(self): | |
self.counter = 0 | |
def get_counter(self): | |
return self.counter | |
counter_controller = CounterControllerPrivate() | |
class CounterControllerPublic(object): | |
def add_one_endpoint(self): | |
counter_controller.add_one() | |
return counter_controller.get_counter() | |
def reset_endpoint(self): | |
counter_controller.reset() | |
return counter_controller.get_counter() | |
return CounterControllerPublic() | |
# counter attribute is not accessible from out here | |
controller = CounterController() | |
print(controller.add_one_endpoint()) | |
print(controller.add_one_endpoint()) | |
print(controller.reset_endpoint()) | |
pprint(dir(controller)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment