title: How to check a condition for every instance method of class?
body: <p>I want to check for an <code>if condition</code> whenever an instance method of a class is called.</p>
  <p>For example,we have constructor classes that have lots of attributes those are used in payload construct methods but they also have <code>payload</code> attribute in which user can pass <code>payload dictionary</code> that can be used directly whenever API request is called.</p>
  <p>For each payload construction function, we have to check at the start of function that</p>
  <pre class="lang-py prettyprint-override"><code>if object.payload:
     return object.payload
  else:
     # construct the payload using other instance variables 
  </code></pre>
  <pre class="lang-py prettyprint-override"><code>&quot;&quot;&quot; Each constructor class has payload attribute &quot;&quot;&quot;
  class X:
      &quot;&quot;&quot; This is a constructor class to represent X object &quot;&quot;&quot;
      def __init__(self, payload=None):
          self.payload = payload
  
  class Y:
      &quot;&quot;&quot; This is a constructor class to represent Y object &quot;&quot;&quot;
      def __init__(self, payload=None):
          self.payload = payload
  
  class CreatePayload:
      def __init__(self, api):
          self.api = api
      
      def create_json_for_x(self, object_x):
          if object_x.payload:
             return object_x.payload
          payload = {
                     &quot;val&quot; : object_x.val
                    }
          return payload
  
      def create_json_for_y(self, object_y):
          if object_y.payload:
             return object_y.payload
          payload = {
                       &quot;val&quot; : object_y.val,
                       &quot;user&quot; : object_y.user
                    }
          return payload
  
      def create_x(self, object_x):
          uri = &quot;/test_x&quot;
          payload = create_json_for_x(object_x)
          # send_request method sends a HTTP GET/PUT/POST/DELETE requests
          request = self.api.send_request(uri, 'post', payload)
          return request
  
       
  
      
  </code></pre>
  <p>I want to have a solution using <code>decorator</code> or <code>Metaclass</code> which takes the object as input and always check if it has the <code>payload</code> attribute then return the <code>payload</code> value.</p>