Skip to content

Commit

Permalink
Test: Simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger committed Nov 24, 2020
1 parent 936533c commit 7111d70
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Doc/howto/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,26 @@ Simple example: A descriptor that returns a constant
----------------------------------------------------

The :class:`Ten` class is a descriptor that always returns the constant ``10``
from its :meth:`__get__` method::
from its :meth:`__get__` method:

.. testcode::

class Ten:
def __get__(self, obj, objtype=None):
return 10

To use the descriptor, it must be stored as a class variable in another class::
To use the descriptor, it must be stored as a class variable in another class:

.. testcode::

class A:
x = 5 # Regular class attribute
y = Ten() # Descriptor instance

An interactive session shows the difference between normal attribute lookup
and descriptor lookup::
and descriptor lookup:

.. doctest::

>>> a = A() # Make an instance of class A
>>> a.x # Normal attribute lookup
Expand Down

0 comments on commit 7111d70

Please sign in to comment.