Skip to content

Instantly share code, notes, and snippets.

@noel9999
Created September 7, 2018 09:23
Show Gist options
  • Save noel9999/76bd666a0e64cb68f5125ba40991910f to your computer and use it in GitHub Desktop.
Save noel9999/76bd666a0e64cb68f5125ba40991910f to your computer and use it in GitHub Desktop.
class Foo(object):
def __init__(self, key):
self.key = key
def __get__(self, obj, type):
if int(obj.__dict__.get(self.key, 0)) > 90:
return 'Great'
else:
return 'Shit'
def __set__(self, obj, value):
if value > 0:
# here shows that __dict__ was an `None` so that can not be assigned.
obj.__dict__[self.key] = value
else:
raise ValueError('Cannot be negative.')
class Bar(object):
score = Foo('score')
def __init__(self):
self.score = 70
def __getattribute__(self, key):
if key is 'score':
return 'from __getattribute__ hi jack with score'
else:
super(Bar, self).__getattribute__(key)
b = Bar()
b.score # here comes an error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment