3

I am looking into Python descriptors. So far, all of the examples I have found on Google assign a descriptor object only class attributes.

Should a descriptor be always be assigned to a class attribute? Can a descriptor be assigned to an instance attribute? If so, any examples or use cases?

For example, all examples are like:

>>> class MyClass(object):  
    x = RevealAccess(10, 'var "x"')  
    y = 5 

Can we have something like:

>>> class MyClass(object):  
    init(self ):  
        x = RevealAccess(10, 'var "x"')  
        y = 5
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Chandu
  • 1,837
  • 7
  • 30
  • 51
  • 2
    What do you mean? A class is an object. You can read about the full protocol here: https://docs.python.org/3/howto/descriptor.html – jonrsharpe Mar 15 '16 at 10:14
  • The following is from the example from the link you pasted >>> class MyClass(object): x = RevealAccess(10, 'var "x"') y = 5 Can we have something like >>> class MyClass(object): init(self ): x = RevealAccess(10, 'var "x"') y = 5 – Chandu Mar 15 '16 at 10:26
  • 3
    We'll have you tried that? What happened? – jonrsharpe Mar 15 '16 at 10:55

0 Answers0