Skip to content

Instantly share code, notes, and snippets.

@llllllllll
Created January 30, 2019 16:53
Show Gist options
  • Save llllllllll/566438890ad0542a3569f6569c90174e to your computer and use it in GitHub Desktop.
Save llllllllll/566438890ad0542a3569f6569c90174e to your computer and use it in GitHub Desktop.
In [1]: class Descr:
...: def __init__(self):
...: self.get_counter = 0
...:
...: def __get__(self, instance, owner):
...: if instance is None:
...: return self
...: self.get_counter += 1
...: return 1
...:
In [2]: class Class:
...: d = Descr()
...:
In [3]: inst = Class()
In [4]: inst.__dict__
Out[4]: {}
In [5]: for n in range(10):
...: inst.d
...:
In [6]: Class.d.get_counter
Out[6]: 10
In [7]: inst.d = 2
In [8]: inst.__dict__
Out[8]: {'d': 2}
In [9]: for n in range(10):
...: inst.d
...:
In [10]: Class.d.get_counter
Out[10]: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment