title: How can I dynamically create class methods for a class in python
body: <p>If I define a little python program as</p>
  <pre><code>class a():
      def _func(self):
          return &quot;asdf&quot;
  
      # Not sure what to resplace __init__ with so that a.func will return asdf
      def __init__(self, *args, **kwargs):
           setattr(self, 'func', classmethod(self._func))
  
  if __name__ == &quot;__main__&quot;:
      a.func
  </code></pre>
  <p>I receive the traceback error</p>
  <pre><code>Traceback (most recent call last):
    File &quot;setattr_static.py&quot;, line 9, in &lt;module&gt;
      a.func
  AttributeError: class a has no attribute 'func'
  </code></pre>
  <p>What I am trying to figure out is, how can I dynamically set a class method to a class without instantiating an object?</p>
  <hr />
  <h2>Edit:</h2>
  <p>The answer for this problem is</p>
  <pre><code>class a():
      pass
  
  def func(cls, some_other_argument):
      return some_other_argument
  
  setattr(a, 'func', classmethod(func))
  
  if __name__ == &quot;__main__&quot;:
      print(a.func)
      print(a.func(&quot;asdf&quot;))
  </code></pre>
  <p>returns the following output</p>
  <pre><code>&lt;bound method type.func of &lt;class '__main__.a'&gt;&gt;
  asdf
  </code></pre>