Editor

Predict the Descriptor Output

Advanced · Predict the output · from Python Descriptors: The Secret Behind @property and ORM Fields

From the lesson: Python Descriptors: The Secret Behind @property and ORM Fields

When you use @property in Python, something interesting happens behind the scenes. The attribute doesn't just sit there waiting to be read - it intercepts access and runs custom code. How does that work?

Read the lesson

What does the following code print?

class Desc:
    def __get__(self, obj, objtype=None):
        return 'descriptor'

class MyClass:
    x = Desc()

obj = MyClass()
obj.__dict__['x'] = 'instance'
print(obj.x)

Think carefully: is Desc a data descriptor or a non-data descriptor?

# Type your predicted output

More Descriptor exercises

All Python exercises · 50 practice problems · Metaprogramming quiz