Sep-04-2021, 05:56 PM
(Sep-04-2021, 05:39 PM)deanhystad Wrote: You should test "==" not "is". Is should only be used when you are testing if two objects are the same instance. Below I make two bytes. They are constructed of the same bytes, but they are different objects.
x = bytes('12345', 'utf-8') y = bytes('12345', 'utf-8') print(x, y, id(x), id(y), x is y, x == y)x and y are equivalent, but they are not the same. x is y returns False and x == y returns True.
Output:b'12345' b'12345' 2409442857008 2409442857104 False True
Just in case rfm9x.receive() returns str, the same is true for them.
x = ''.join(['a', 'b', 'c']) y = ''.join(['a', 'b', 'c']) print(x, y, id(x), id(y), x is y, x == y)You may wonder why I constructed the strings as I did. The reason for using join instead of a string literal is Python is clever and tries to be efficient. This code does not create two str objects.
Output:abc abc 3078969735600 3078969723888 False True
x = 'abc' y = 'abc' print(x, y, id(x), id(y), x is y, x == y)In this example not only does x == y, but x is y. Notice that the object id for x and y are the same.
Output:abc abc 1811113886448 1811113886448 True True
thanks for the == tip
this worked for me...
onset = bytes("onset", "utf-8") unset = bytes("unset", "utf-8") while True: packet = rfm9x.receive(timeout=5) if packet is None: led.value = False print("Received nothing! Listening again...") elif packet == onset: print("its on") elif packet == unset: print("its off") else: led.value = True print("Received (raw bytes): {0}".format(packet)) packet = str(packet, "ascii")