Python ctypes.BigEndianStructure can't store a value
Question: I am in trouble with ctypes.BigEndianStructure. I can't get the value that I
set to one the fields. My code is like this.

    
    
    import ctypes
    class MyStructure(ctypes.BigEndianStructure):
        _pack_ = 1
        _fields_ = [
            ('fx', ctypes.c_uint, 7),
            ('fy', ctypes.c_ubyte, 1)
        ]
    
    x = MyStructure()
    

It prints 0 as excepted:

    
    
    print x.fy  # Prints 0
    

then I set a value to it but it still prints 0:

    
    
    x.fy = 1
    print x.fy  # Still prints 0
    


Answer: I don't know why what your doing doesn't work and it is certainly strange
behavior. I think this alternative code works.

    
    
    import ctypes
    class MyStructure(ctypes.BigEndianStructure):
        _pack_ = 1
        def __init__(self):
            self.fx=ctypes.c_uint(7)
            self.fy = ctypes.c_ubyte(1)
    
    x = MyStructure()
    x.fy = 7
    print x.fy # prints 7
    

Or without the constructor::

    
    
    import ctypes
    class MyStructure(ctypes.BigEndianStructure):
        _pack_ = 1
        fx = ctypes.c_uint(7)
        fy = ctypes.c_ubyte(1)
    
    x = MyStructure()
    x.fy = 7
    print x.fy # prints 7
    

I have personally never used the _fields_ attribute so I can't speak to the
odd behavior.

