O'Reilly logo
  • Chris Kang thinks this is interesting:

c = Const(23)


Cover of Python in a Nutshell, 3rd Edition


"c" is a special attribute of class "X", which we call "descriptor". This attributes has special get & set behaviour as defined by the class Const, which defines the descriptor class. When we want the special get/set behaviour on an attribute, we create a descriptor type attribute, not a regular attribute type for which special get/set is not defined. Note that you can ONLY set an descriptor on an object, but you can do get on both class and object that has the descriptor attribute. eg. If X.c = 42 is not allowed, but x.c = 42 is allowed.