Last active
August 29, 2015 14:23
-
-
Save EricsonWillians/70d43c84a33498cb69b9 to your computer and use it in GitHub Desktop.
Silly Private Class Attempt 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
# Silly private class implementation attemp in Python, by Rederick Deathwill. | |
class PrivateClass(): | |
def __init__(self, **kwargs): | |
self.vars = kwargs | |
def __getattr__(self, attr): | |
for key in self.vars["private"]: | |
if key == attr: | |
raise RuntimeError("You're not allowed to access a private variable ('{0:s}').".format(attr)) | |
for key in self.vars["public"]: | |
return self.vars["public"][key] if key == attr else None | |
x = PrivateClass(public = {"var0": "Public Var"}, private = {"var1": "Private Var"}) | |
print(x.var0) # Allowed access, var0 is public. | |
print(x.var1) # Access denied, var1 is private. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment