Using decorators with argument in Python

John Posner jjposner at codicesoftware.com
Fri Jul 1 17:18:29 CEST 2011


On 2:59 PM, Ethan Furman wrote:

<snip>

>     def __call__(self, func=None):
>         if func is None:
>             return self._call()
>         self.func = func
>         return self
>     def _call(self):
>         print("\n" + self.char * 50)
>         self.func()
>         print(self.char * 50 + '\n')
>

I believe the "if" block should be:

  if func is None:
      self._call()
      return

Or perhaps the _call() method should be revised:

  def _call(self):
      print("\n" + self.char * 50)
      retval = self.func()
      print(self.char * 50 + '\n')
      return retval

-John




More information about the Python-list mailing list