title: Implementing __getattr__ inside a class method such as __call__
body: <p>So I've created a module inspired heavily by <a href="https://github.com/amoffat/sh" rel="nofollow noreferrer">amoffat's sh module</a>, where I can import shell programs as functions; unlike <code>sh</code>, my module can do something like <code>git(C = path).commit(m = message)</code> directly, by returning the module class itself as a partial: <code>return partial(bakery, self.program)</code>. However, I've lost the ability to run something like <code>ls()</code> without a placeholder method like <code>ls._()</code>, which doesn't look as good. The code in the latter: <code>return output_as_list(args, kwargs)</code>.</p>
  
  <pre class="lang-py prettyprint-override"><code>from functools import partial
  
  def __getattr__(name):
      if name == "__path__":
          raise AttributeError
      return bakery(name)
  
  class bakery:
      def __init__(self, program):
          self.program = program
      def __getattr__(self, subcommand):
          return subcommand
      @property
      def __call__(self):
          return partial(bakery, self.program)
  </code></pre>
  
  <p><strong>My question is this:</strong></p>
  
  <p>Is there a way to tell <code>__call__</code> you're accessing a dynamic attribute, using a <code>__getattr__</code> inside it, for example, to implement both the <code>git(C = path).commit(m = message)</code> and <code>ls()</code> scenarios? Or to conditionally return a partial or an output list depending on whether an attribute of <code>__call__</code> is being accessed?</p>
  
  <hr>
  
  <h2>Edit:</h2>
  
  <p>I was wondering if something similar to this might work?</p>
  
  <pre class="lang-py prettyprint-override"><code>def __call__(self, *args, **kwargs):
      class inner_class:
          def __init__(self, *args, **kwargs):
              self.args = args
              self.kwargs = kwargs
          def __getattr__(self, subcommand):
              return partial(bakery, self.program)
          def __call__(self, *args, **kwargs):
              return bakery(*self.args, **self.kwargs)._get_output_as_list(*args, **kwargs)
  
      return inner_class(self.program, *args, **kwargs)
  </code></pre>
  
  <hr>
  
  <h2>Edit 2:</h2>
  
  <p>I suppose I could just convert the individual <code>__call__</code> functions to subclasses, and import from whichever is necessary.</p>