-3

c++ type structure example:

Struct xyz {
   uint64_t a;
   uint32_t b;
   uint16_t c;
   bool     d;
   char     e;
} var;

where I can access it using var.a = 1; var.b = 2, and so on...

Here I have a array to initialize this struct, like take a list of python:

values = [0x64, 0x32, 0x16, true, 'a'];

I need to map this list value to struct elements, So that it can be accessed using var.a if print var.a it should print 0x64

Kindly suggest how entire scenario can be implemented in python in most efficient way. What I have implemented is as like below:

Class xyz(Structure):
   __field__ [ 
               (a, c_uint64, 64),
               (b, c_uint32, 32),
               (c, c_uint16, 16),
               (d, c_bool),
               (e, c_char)
             ]
    def __init__(self, p, q, r, s, t):
        self.a = p
        self.b = q
        self.c = r
        self.d = s
        self.e = t

obj = xyz(10, 11, 12, True, 'R')

It works fine, whether this is the proper usage or Is there some better option. This also gives me privilege for bi fields might be the case where I need int of 14 bits.

ronex dicapriyo
  • 161
  • 1
  • 2
  • 12
  • 1
    Use `class` instead of struct?! – shad0w_wa1k3r Nov 14 '13 at 18:22
  • possible duplicate of [C-like structures in Python](http://stackoverflow.com/questions/35988/c-like-structures-in-python) – shad0w_wa1k3r Nov 14 '13 at 18:23
  • looks really strait forward to me, what problems are you having? – cmd Nov 14 '13 at 18:24
  • Please edit the question accordingly. – shad0w_wa1k3r Nov 14 '13 at 18:33
  • 1
    @cmd, I need some proper example to start with which do all the basic task I mentioned otherwise It'd be like I keep on wondering over net on different unusual stuffs. – ronex dicapriyo Nov 14 '13 at 18:33
  • Take a look at http://stackoverflow.com/questions/7468168/python-equivalent-of-c-struct-porting-app-form-c-to-python?rq=1 – shad0w_wa1k3r Nov 14 '13 at 18:35
  • @AshishNitinPatil will you please stop sharing the link,before asking question I already have search this stuffs, And If possible explain the same in some understandable terms. I found both of that not at my level and also couldn't fulfill my requirement. And also please up-rate the question as you have down rated it. And I am more specific in my question. – ronex dicapriyo Nov 14 '13 at 18:39
  • I am but trying to help. Please control your temper. You could refine your question accordingly, so only I shared those links. Self-help is the best help is all I would add. – shad0w_wa1k3r Nov 14 '13 at 18:43
  • This question, and countless others, are answered by [The Python Tutorial](http://docs.python.org/2/tutorial/). – Robᵩ Nov 14 '13 at 19:09

1 Answers1

4

In Python, we tend to use classes in place of structs (assuming you're looking to create structs so you can bundle together related data in a single data structure)

class xyz(object):
    def __init__(self, a, b, c, d, e):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.e = e

>>> my_var = xyz(0x64, 0x32, 0x16, True, 'a')
>>> print my_var.a
100

If this is too verbose, you could try using namedtuples, which create a sort of mashup between a class and a tuple in a line of code

>>> from collections import namedtuple
>>> xyz = namedtuple('xyz', ['a', 'b', 'c', 'd', 'e'])  # Creates an 'xyz' class
>>> my_var = xyz(0x64, 0x32, 0x16, True, 'a')  # Creates an instance of that class
>>> print my_var.a
100

If you want to map a list of elements to your class, you can do the following (using either classes or named tuples):

>>> vars = [0x64, 0x32, 0x16, True, 'a']
>>> my_var = xyz(*vars)
>>> print my_var.a
100

There's really no type-checking in Python that's analogous to how C does it. Members in a class don't have an inherent type of any kind -- they're more like boxes which you can place arbitrary data types into. (C is statically, but weakly typed, whereas Python is dynamically, but strongly typed).

I suppose you could use a combination of properties and maybe decorators to create your own type-checking, but in the vast majority of cases, that's overkill.


If you want to create a struct so you can literally convert between Python values and C structs to manipulate binary data, consider using the struct module, which allows you to create and manipulate c-style structs specifically for purposes of interoperability.

If you're just looking for ways to simply organize and encapsulate your code, the struct module would not be a good choice.

sinistersnare
  • 114
  • 1
  • 1
  • 11
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • 1
    @ronex dicapriyo, please make your own answer if you have found something that works for you, instead of trying to [change another's answer](http://stackoverflow.com/review/suggested-edits/3366675), or trying to [change it for a second time](http://stackoverflow.com/review/suggested-edits/3366700). – gunr2171 Nov 15 '13 at 16:25
  • @gunr2171 That option is not available for me. – ronex dicapriyo Nov 15 '13 at 16:29
  • 1
    @ronexdicapriyo, your question is on hold until you can edit your question to reopen it. At that point you can then add your own answer. – gunr2171 Nov 15 '13 at 16:30