Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Why do I need the descriptor class? Please explain using this example or the one you think is better.</p> </blockquote> <p>it gives you extra control over how attributes work. if you're used to getters and setters in java, for example, then it's python's way of doing that. one advantage is that it looks to users just like an attribute (there's no change in syntax). so you can start with an ordinary attribute and then, when you need to do something fancy, switch to a descriptor.</p> <p>an attribute is just a mutable value. a descriptor lets you execute arbitrary code when reading or setting (or deleting) a value. so you could imagine using it to map an attribute to a field in a database, for example - a kind of ORM.</p> <p>another use might be refusing to accept a new value by throwing an exception in <code>__set__</code> - effectively making the "attribute" read only.</p> <blockquote> <p>What is instance and owner here? (in <code>__get__</code>). So my question is, what is the purpose of the third parameter here?</p> </blockquote> <p>this is pretty subtle (and the reason i am writing a new answer here - i found this question while wondering the same thing and didn't find the existing answer that great).</p> <p>a descriptor is defined on a class, but is typically called from an instance. when it's called from an instance both <code>instance</code> and <code>owner</code> are set (and you can work out <code>owner</code> from <code>instance</code> so it seems kinda pointless). but when called from a class, only <code>owner</code> is set - which is why it's there.</p> <p>this is only needed for <code>__get__</code> because it's the only one that can be called on a class. if you set the class value you set the descriptor itself. similarly for deletion. which is why the <code>owner</code> isn't needed there.</p> <blockquote> <p>How would I call/ use this example?</p> </blockquote> <p>well, here's a cool trick using similar classes:</p> <pre><code>class Celsius: def __get__(self, instance, owner): return 5 * (instance.fahrenheit - 32) / 9 def __set__(self, instance, value): instance.fahrenheit = 32 + 9 * value / 5 class Temperature: celsius = Celsius() def __init__(self, initial_f): self.fahrenheit = initial_f t = Temperature(212) print(t.celsius) t.celsius = 0 print(t.fahrenheit) </code></pre> <p>(i'm using python 3; for python 2 you need to make sure those divisions are <code>/ 5.0</code> and <code>/ 9.0</code>). that gives:</p> <pre><code>100.0 32.0 </code></pre> <p>now there are other, arguably better ways to achieve the same effect in python (eg if celsius were a property, which is the same basic mechanism but places all the source inside the Temperature class), but that shows what can be done...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload