title: "Unbound Local Error" in a decorator
body: <p>I'm getting an error that has me pretty stumped. </p>
  
  <pre><code>#! /usr/bin/env python3
  from sys       import modules
  from inspect   import isclass
  from functools import partial
  from abc       import abstractmethod
  from abc       import ABC as AbstractClass
  
  
  class Decoration(AbstractClass):
      defaults = {}
  
      def __init__(self, *list, **map):
          self.list = list
          self.map = map
  
      def __get__(self, caller, owner):
          return partial(self.__call__, caller)
  
      @abstractmethod
      def __call__(self, decoratee, *list, **map):
          pass
  
      @staticmethod
      def set_defaults(defaults):
          Decoration.defaults = defaults
  
  
  class Decorator(Decoration):
  
      def __init__(self, *list, **map):
          super().__init__(self, *list, **map)
  
      def __call__(self, function, *list, **map):
          print(function)
          def decorated(*list, **map):
              if map:
                  for decorator in map:
                      for arguments in map[decorator]:
                          function = getattr([__name__], decorator)(function, arguments)
              return function(*list, **map)
          return decorated
  
  
  @Decorator()
  def dummy(*list, **map):
      for item in list:
          print(item)
      for k, v in map:
          print('{} {}'.format(k, v))
  
  
  def main():
      dummy({'banana': True})
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>On the "function = getattr ..." line, I'm getting this error:</p>
  
  <p><code>UnboundLocalError: local variable 'function' referenced before assignment</code></p>
  
  <p>I'm confused mainly because the print statement above finds the function's definition as</p>
  
  <p><code>&lt;function dummy at (address)&gt;</code></p>
  
  <p>which is correct. Furthermore, deleting that line lets the program run fine and return currently on the next line. As far as I can tell, it's not a syntax problem of getattr. Any ideas?</p>