Can't get attribute blynk inside of @blynk decorator method

Hello,
I want to rearrange my code to become object oriented so I can reuse to my another project. I am using raspberry pi and this machine recommend me to use python. I am not an python expert because I think this question more like python question rather than blynk question.

import blynklib

class blynk_input(Base):
    pass
    auth_token= 'my token was here...'
    blynk = blynklib.Blynk(auth_token)
    input_throttle = 0
    input_yaw = 0
    input_roll = 0
    input_pitch = 0

    @blynk.handle_event("connect")
    def connect_handler():
        for pin in range(4):
            print('Syncing virtual pin {}'.format(pin))
            blynk.virtual_sync(pin)

            # within connect handler after each server send operation forced socket reading is required cause:
            #  - we are not in script listening state yet
            #  - without forced reading some portion of blynk server messages can be not delivered to HW
            blynk.read_response(timeout=0.3)

so I made a class called blynk_input and whatever written here is to communicate with blynk data.
and then I made a subclass of this to handle specific algorithm in my project:

from blynk_aeromodel import blynk_input

class elevon(blynk_input):
    pass
    
    #raspberry pi hardware pwm only has 2 channel,
    #gpio bcm 13 and gpio bcm 19 is in same channel
    #gpio bcm 12 and gpio bcm 18 is in same channel
    rudderPin = 27 #software
    throttlePin = 22 #software
    elevonLPin = 18 #hardware
    elevonRPin = 13 #hardware

    #initiation purpose
    input_elevonL = 0
    input_elevonR = 0
    hw_midValue = 0.0
    sw_midValue = 0.0
    input_elevonTrigger = False
    

    def run(self):
        print("make sure daemon pigpiod already running (sudo pigpiod)")
        print("rudder pin: " +str(self.rudderPin))
        try:
            #software pwm
            self.init_SW_PWM("Throttle software pwm", self.throttlePin, 50, self.setSWAngletoPWM(90))
            self.init_SW_PWM("Rudder software pwm", self.rudderPin, 50, self.setSWAngletoPWM(90))

            #hardware pwm
            self.init_HW_PWM("Elevon L hardware pwm", self.elevonLPin, 50, self.setHWAngletoPWM(90))
            self.init_HW_PWM("Elevon R hardware pwm", self.elevonRPin, 50, self.setHWAngletoPWM(90))

            #set mid value point to 90
            #untuk kalibrasi algoritma elevon
            self.hw_midValue = self.setHWAngletoPWM(90)
            self.sw_midValue = self.setSWAngletoPWM(90)
            while True:
                self.blynk.run()
                print("roll :"+ str(input_roll))
        finally:
            self.gpio.stop()

as written above, I able to call member blynk in blynk_input class by call self.blynk.run()
and then I just instantiate in my main class:

from elevon_model import elevon

class main_aero(Daemon):
    def run(self):
        # this is only snip code
        aero = elevon()
        aero.run()

it’s running.
however, I got an error once I give an internet connection. this is error message:

self._events[event](*args, **kwargs)
  File "/home/pi/python_script/aeromodel/blynk_aeromodel.py", line 17, in connect_handler
    blynk.virtual_sync(pin)
NameError: global name 'blynk' is not defined

blynk will trigger @blynk.handle_event("connect") however I can’t refer my instance which is usually defined in parameter as self. I don’t know how blynk decorator working but looks like it’s running outside of my instance. this is another error if I added self manually on handler:

 self.call_handler(self._CONNECT)
  File "/usr/local/lib/python2.7/dist-packages/blynklib.py", line 343, in call_handler
    self._events[event](*args, **kwargs)
TypeError: connect_handler() takes exactly 1 argument (0 given)

nevermind, I have recognized that @blynk decoration will be handled as global method. So then I just call my variable with my class name. like: blynk_input.blynk.virtual_sync(pin).

looks like I just need to learn more about decorator pattern

1 Like