Monday, 2 September 2013

Bitwise operations on constants within Class not working as expected

Bitwise operations on constants within Class not working as expected

I'm trying to do some bitwise operations on some constants within a class
and for some reason, the computed value is not being stored.
Here's what I'm doing:
class FramePacket(object):
def __init__(self,frame):
self._frame = frame
def create_packet(self):
# Frame ID
ID = 0x7
# Commands
CMD_PING = 0x0
CMD_STORE = 0x1
CMD_PLAY = 0x2
CMD_DEMO = 0x3
CMD_CLEAR = 0xE
CMD_WIPE = 0xF
f_data = (ID << 4) + CMD_STORE
print "f_data ---> " + str(f_data)
When I execute this, I get an error and see this in the traceback:
print "f_data ---> " + str(f_data)
NameError: name 'f_data' is not defined
However, when I execute this in the Python interpreter, without using a
class, it works as expected:
>>> CMD_STORE = 0x1
>>> ID = 0x7
>>> f_data = (ID << 4) + CMD_STORE
>>> print "f_data ---> " + str(f_data)
f_data ---> 113
I'm missing something but not sure what...
Thanks.

No comments:

Post a Comment