WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Advanced · question 56 of 100

What is a descriptor in Python, and how do you create one? Provide examples of using descriptors for efficient attribute management.?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

In Python, a descriptor is a mechanism that allows objects to define how they handle attribute access. A descriptor is an object attribute with one or more of the following special methods: __get__(), __set__(), or __delete__(). These methods define how the descriptor behaves when it is accessed as an attribute of an object. Descriptors can be used to manage attributes in a way that is more efficient or flexible than using simple instance variables or properties.

To create a descriptor, you define a class with one or more of the special methods mentioned above. For example, consider a descriptor that converts an attribute value from inches to centimeters:

    class InchToCmDescriptor:
        def __get__(self, instance, owner):
            return instance._inches * 2.54
        
        def __set__(self, instance, value):
            instance._inches = value / 2.54
    
    class Height:
        height_cm = InchToCmDescriptor()
        
        def __init__(self, inches):
            self._inches = inches

In this example, InchToCmDescriptor is a descriptor class with __get__() and __set__() methods. The Height class defines an instance variable _inches, which is used by the descriptor to calculate the height in centimeters. The height_cm attribute of the Height class is an instance of the InchToCmDescriptor descriptor.

Now we can create an instance of the Height class and access its height_cm attribute:

    >>> h = Height(70)
    >>> h.height_cm
    177.8
    >>> h.height_cm = 180
    >>> h._inches
    70.86614173228347

By using a descriptor, we can convert between inches and centimeters transparently, without having to write separate methods for getting and setting the values.

Descriptors can also be used for other purposes, such as validation, caching, or lazy loading. For example, a caching descriptor might store the result of a method call in an instance variable, so that subsequent calls with the same arguments return the cached result instead of recomputing it. A lazy loading descriptor might only load an attribute value from a database or file when it is first accessed, instead of loading all attribute values at object initialization. Descriptors can be a powerful tool for managing object attributes in a flexible and efficient way.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics