Tracy Gao
Tracy Gao

Reputation: 129

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.

Upvotes: 1

Views: 1027

Answers (1)

Martin Evans
Martin Evans

Reputation: 46759

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

Upvotes: 2

Related Questions