---
title: "Descriptor Decorator in Python"
description: "Combining Python decorators with descriptor protocol. Practical notes and code examples on Descriptor Decorator in Python."
url: "https://h3manth.com/scribe/descriptor-decorator-in-python/"
canonical_url: "https://h3manth.com/scribe/descriptor-decorator-in-python/"
markdown_url: "https://h3manth.com/scribe/blog/descriptor-decorator-in-python.md"
publishDate: "2014-12-01T00:00:00.000Z"
author: "Hemanth HM"
category: "python"
tags: ["python"]
word_count: 73
reading_time_minutes: 1
x_markdown_tokens: 259
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Descriptor Decorator in Python

> Combining Python decorators with descriptor protocol. Practical notes and code examples on Descriptor Decorator in Python.

[Decorators](https://www.python.org/dev/peps/pep-0318/) and [Descriptors](https://docs.python.org/2/howto/descriptor.html) gel together beautifully.

## Create an Accessor Descriptor

```python
class Accessor(object):
    def __init__(self, fget):
        self.fget = fget
    
    def __get__(self, obj, type):
        if obj is None:
            return self
        return self.fget(obj)

## Use as Decorator
python
class Greet():
    msg = "meow"
    
    @Accessor
    def yo(self):
        return "Yo!"
```

Now `greetThem.yo` returns `"Yo!"` without calling `()`.

**The Magic:** When Python finds a descriptor in the class dictionary, it executes it instead of returning it!
