Is there a way to apply decorators to Python data structures?
<p>From what I got so far, decorators applies to callables(functions and classes).
Wondering if there is a way to apply a decorator to a dictionary?(It can be any data structure, in fact).
The problem I try to solve goes along the following lines:
I have a lot of dictionaries which, from time to time, might be valid or not.</p>

<p>I want to mark them with a <em>decorator-like</em> as shown in the skeleton bellow...:</p>

<pre><code>class Data(object):

    def invalide(self):
        return False

    def valide(self, some_dict):
        return some_dict

@Data.invalide  
dict_1 = {...}

@Data.valide  
dict_2 = {...}

@Data.valide  
dict_3 = {...}
...
...
@Data.invalide
dict_n = {...}
</code></pre>

<p>...so, when I call <code>some_function(dict_x)</code>, it would know to do one thing or the other based on the <code>valide</code>, <code>invalide</code> marks.</p>

<h2>-------------Later on--------------------</h2>

<p>I ended up implementing the following:</p>

<pre><code>In [2]: class Data(object):
   ...:     
   ...:     @classmethod
   ...:     def valide(self, func):
   ...:         #print "Data is valide."
   ...:         return func()
   ...:     
   ...:     @classmethod
   ...:     def invalide(self, func):
   ...:         #print "Data is invalide."
   ...:         return False
   ...:     

In [3]: @Data.valide
   ...: def dict1():
   ...:     return {"a": 1, "b": 2, "c": 3}
   ...: 
   ...: @Data.invalide
   ...: def dict2():
   ...:     return {"d": 4, "e": 4, "f": 6}
   ...: 
   ...: def run_funct(some_dict):
   ...:     return some_dict
   ...: 

In [4]: print(run_funct(dict1))
   ...: print type(run_funct(dict1))
   ...: 
{'a': 1, 'c': 3, 'b': 2}
&lt;type 'dict'&gt;

In [5]: print(run_funct(dict2))
   ...: print type(run_funct(dict2))
   ...: 
False
&lt;type 'bool'&gt;
</code></pre>

<p>Decorators, to my knowledge, can only be applied to callables (functions, classes etc.)</p>

<p>If I was in your place, I'd probably separate the logic to decide between valid/invalid in its own function.</p>

python