Skip to content

Instantly share code, notes, and snippets.

@judy2k
Created September 1, 2016 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judy2k/634c5e3af1c9498f680178352b5f26eb to your computer and use it in GitHub Desktop.
Save judy2k/634c5e3af1c9498f680178352b5f26eb to your computer and use it in GitHub Desktop.
Demonstrates that data descriptors have precedence over the instance dict.
class NonDataDescriptor(object):
def __get__(self, *args, **kwargs):
return 'magic'
class DataDescriptor(object):
def __get__(self, *args, **kwargs):
return 'magic'
def __set__(self, *args, **kwargs):
pass
class MyObject(object):
ndd = NonDataDescriptor()
dd = DataDescriptor()
def __init__(self):
self.ndd = "non-data"
self.dd = "data"
ob = MyObject()
print(ob.ndd)
print(ob.dd)
@judy2k
Copy link
Author

judy2k commented Sep 2, 2016

Output:

$ python2 magic.py
non-data
magic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment