Skip to content

Instantly share code, notes, and snippets.

@EricsonWillians
Last active August 29, 2015 14:23
Show Gist options
  • Save EricsonWillians/70d43c84a33498cb69b9 to your computer and use it in GitHub Desktop.
Save EricsonWillians/70d43c84a33498cb69b9 to your computer and use it in GitHub Desktop.
Silly Private Class Attempt in Python
# 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