简体   繁体   中英

How to check whether the value of a variable is updated or not in Python

I am new to the python and here I have a quick question.

say in a while loop, I have a variable called center, and center is a list, as shown above:

     M = cv2.moments(c)
    center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

center will always be [x,y] and since M is dynamically changed all the time, so does the center.

My question is how can I know whether center is updated or not? If it is updated, how can I mathematically compare this the new center with the previous center?

Thanks.

You need to keep track of center . To do this create a variable to hold the previous value. This can be initialised to None at the start as you don't have a previous value. For example:

previous_center = None

while True:    

    #
    # Your loop code
    #

    M = cv2.moments(c)
    center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

    if center != previous_center:
        print("New center", center)
        previous_center = center

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM