Python ProgrammingPython Programming

Python decorator get class instance

class MySerial():
  def __init__(self):
    pass  # I have to have an __init__

  def write(self, *args):
    print(args[0])
    pass  # write to buffer

  def read(self):
    pass  # read to buffer

  @staticmethod
  def decorator(func):
    def func_wrap(cls, *args, **kwargs):
      cls.ser.write(func(cls, *args, **kwargs))
      return cls.ser.read()
    return func_wrap


class App():
  def __init__(self):
    self.ser = MySerial()

  @MySerial.decorator
  def myfunc(self):
    self = 100
    return ['canada', 'australia']


App().myfunc()
Output
['canada', 'australia']