title: How to modify class object in decorator function
body: <p>First of all, I'm using scrapy Module and now I need customized Items.
  Basically I'm trying to get this form without changing the structure of the methods:</p>
  <blockquote>
  <p>{&quot;outer&quot;: {&quot;inner_1&quot;: &quot;value1&quot;, &quot;inner_2&quot;: &quot;value2&quot;}}</p>
  </blockquote>
  <p>So whenever one of the methods with the decorator &quot;is_in_outer&quot; is called, it should check if the &quot;outer&quot; key is already in current item. Or else it would raise a Key Error. I would also appreciate another solution without a decorator, which still does the same thing.
  Thank you for reading and hope someone can help me :)</p>
  <pre><code>class MyItem(scrapy.Item):
  
      def is_in_outer(cls):
          if &quot;outer&quot; not in cls._values.keys():
              cls.__setitem__(&quot;outer&quot;, {})
  
      def __setitem__(self, key, value):
          self._values[key] = value
          self.fields[key] = {}
  
      def __getitem__(self, key):
          return self._values[key]
  
      @is_in_outer
      def set_inner_1(self, value):
          self._values[&quot;outer&quot;][&quot;inner_1&quot;] = value
  
      @is_in_outer
      def set_inner_2(self, value):
          self._values[&quot;outer&quot;][&quot;inner_2&quot;] = value
  </code></pre>