Created
September 7, 2018 09:23
-
-
Save noel9999/76bd666a0e64cb68f5125ba40991910f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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