当变量数据改变时做一些事情

Do something when variable data was changed

我正在尝试制作一个项目,如果更改了可变数据,该项目将发送一些文本。 但我一直在倾听变量的变化。 这是我的代码:

# potentiometer.py

import serial
import time
from discord_webhook import DiscordWebhook

# make sure the 'COM#' is set according the Windows Device Manager
ser = serial.Serial('COM8', 9800, timeout=1)
time.sleep(2)
while True:
    for i in range(50):
        line = ser.readline()   # read a byte
        if line:
            string = line.decode()  # convert the byte string to a unicode string
            num = int(string) # convert the unicode string to an int
            print(num)
        #code to execute when num changes
        #webhook = DiscordWebhook(url='your webhook url', content='Webhook Message')
        #response = webhook.execute()
ser.close()

我需要一些帮助

import serial
import time
from discord_webhook import DiscordWebhook

# make sure the 'COM#' is set according the Windows Device Manager
ser = serial.Serial('COM8', 9800, timeout=1)
time.sleep(2)
while True:
    prev_num = 0
    for i in range(50):
        line = ser.readline()   # read a byte
        if line:
            string = line.decode()  # convert the byte string to a unicode string
            num = int(string) # convert the unicode string to an int
            print(num)
            if num != prev_num:
                prev_num = num 
                webhook = DiscordWebhook(url='your webhook url', content='Webhook Message')
                response = webhook.execute()

ser.close()