1

Is there a way in python to compare a variable with itself? Basically, I am trying to check if the value being fed to a variable 'X' is changed or not.

In more simpler terms, e.g.,

The value of X at 10:30 AM is = 23

The value of X at 10:40 AM is = 23 (It can be the same )

The value of X at 10:50 AM is = 30 (It can change after sometime)

So I want to know how to monitor this change in variable 'X' using python?

I hope my question is clear.

Thanks in advance !

  • Are you checking at specific interval or at random? say x is 23 while processing step 5 in the program and when it reaches step 7, it gets to be 30. Do you want to know about it? Do you code that is looking for this check? What's the usecase so we can solve for it – Joe Ferndz Jan 22 '21 at 05:39
  • It sounds to me like you should have an object responsible for tracking changes to x, with a boolean flag which gets updated when x changes. – kaya3 Jan 22 '21 at 07:47
  • Hi I will tell you the usecase of this. I am trying to develop read / write functionality from Modbus to OPCUA protocol. Read from Modbus and write to OPCUA . And vice versa. For write back case, I thought of comparing the change in value in opc variable. Only when there is a change in the opc variable, I would run a function to write back the value to modbus. For this purpose, I wanted to understand the ways of performing this comparing operation. Apologies, if I have not explained the requirement clearly – shrivdya rao Jan 22 '21 at 08:14

5 Answers5

0

Why don't you simply store x in oldx variable at the end of the process using oldx = x? If x is not a simple type type but a list or something like that which creates more references when using =, then you can import copy module and do:

import copy

#Before process
oldx = copy.deepcopy(x)

#Process that may change x



#Compare x after process
if oldx = x:
Shambhav
  • 813
  • 7
  • 20
  • Unfortunately, this will not work. OP is just trying to check for value of x based on time delay. There is a potential that oldx and x will remain same if the time did not pass the time window (eg 5 mins) – Joe Ferndz Jan 22 '21 at 05:37
  • @Joe Ferndz But there must be a certain process that checks the time and changes x, it can't just randomly happen. – Shambhav Jan 22 '21 at 05:53
0

You can put the original value of x in another variable before changing its value.

x = 12
y = x
x = 32 # Change your x value after declaring y
if x == y:
    print("Changed")
else: 
    print("Not changed")
ani
  • 303
  • 3
  • 15
0

Well, I believe watchpoints is the perfect library you need in this case.

watchpoints can monitor any variable and notify you through print by default, or any customized callback.

For example:

from watchpoints import watch
a = 10  # This is the initial value
watch(a)  # Now you will monitor all changes to this variable
# Some code that won't change a here
a = 20  # a is changed, and you will get some detailed info in your stdout

Something like:

====== Watchpoints Triggered ======
Call Stack (most recent call last):
  <module> (my_script.py:5):
>   a = 20
a:
10
->
20

It will tell you on which file, which line, the variable is changed, the previous value and the current value. If you need more than print, you can define your own callback function. Just check out https://github.com/gaogaotiantian/watchpoints for it's usage.

minker
  • 510
  • 6
  • 3
0
from watchpoints import watch
a = 10  # This is the initial value
watch(a)  # Now you will monitor all changes to this variable
# Some code that won't change a here
a = 20  # a is changed, and you will get some detailed info in your stdout

I think this could help you, but I need to ask if someone knows how to compare the variable. like which one is higher and print the higher one

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 31 '22 at 04:09
-3

try like this:

if x == x:
   print("value changed")
else:
   print("not changed")

you can modify it as you want. It's just an example ;)

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • 1
    unfortunately, when x value gets changed, x will always be equal to x. The ask is to check with the previous value of x with a time lapse. In other words, value of x 5 minutes ago vs. value of x at the current time. If x was 10 and after 5 mins x got changed to 20, then x will always be equal to x. You need to capture the value of x in another variable and check (with time delay added). So this solution will NOT work. – Joe Ferndz Jan 22 '21 at 05:35