2

Is there anyway to support structures in python and does it not support the normal keyword struct?

for eg:

struct node
{
  unsigned dist[20];
  unsigned from[20];
}rt[10];

How can i covert this into a python struct?

4
  • 1
    Just as a side note: If you want to make python interact with c data types you should have a look at the ctypes module. Commented Apr 22, 2013 at 16:18
  • Calling the struct keyword "normal" indicates to me that you are thinking strictly in terms of a line-by-line translation of C into Python. I suggest at the very least you start by reading a tutorial on Python programming. Commented Apr 22, 2013 at 16:21
  • 1
    @chepner no i mean emulating c-structures Commented Apr 22, 2013 at 17:16
  • So do I, unless you are looking for the struct package. Commented Apr 22, 2013 at 17:34

4 Answers 4

8

I think the Python's equivalent to C-structs is classes:

class Node:
    def __init__(self):
        self.dist_ = []
        self.from_ = []

rt = []
Sign up to request clarification or add additional context in comments.

11 Comments

from is a keyword in Python; that'll give a SyntaxError.
Generally to avoid name clashes with built-in you should use trailing underscores, since leading underscore are conventioanlly used to indicate "private" variables.
@Bakuriu: Thanks. See my edited post. Is this a PEP8's convention or something?
Exactly. See here. In particular "_single_leading_underscore: weak "internal use" indicator. " and "single_trailing_underscore_: used by convention to avoid conflicts with Python keyword"
Thanks.So do i just use dist and from as lists?
|
3

Since the ordering of attributes (unless an OrderedDict or something is used to __prepare__ or otherwise build the class) is not necessarily in order of definition, if you wanted to be compatible with an actual C struct or rely on data being in some order, then the following is a base you should be able to use (using ctypes).

from ctypes import Structure, c_uint

class MyStruct(Structure):
    _fields_ = [
        ('dist', c_uint * 20),
        ('from', c_uint * 20)
    ]

1 Comment

@AbhishekHerle It might be best if you had a read through docs.python.org/2/library/ctypes.html - that'll give you a decent overview
1

Even an empty class would do:

In [1]: class Node: pass
In [2]: n = Node()
In [3]: n.foo = [1,2,4]
In [4]: n.bar = "go"
In [8]: print n.__dict__
{'foo': [1, 2, 4], 'bar': 'go'}
In [9]: print n.bar
go

1 Comment

Adding attributes in this way, although possible, is mostly a bag practice. Anyway, in python3.3+ you don't even have to create the empty class. Simple use SimpleNamespace.
1

@Abhishek-Herle

If I would have been in your situation, I might rely on Struct module in python.

Like, in your case C structure is:

struct node
{
  unsigned dist[20];
  unsigned from[20];
}rt[10];

So here basic idea is to convert C-Structure to python and vice a versa. I can roughly define above c-structure in below python code.

s = struct.Sturct('I:20 I:20') 

Now, if I want to pack any values to this structure I can do, some thing like below.

dist = [1, 2, 3....20]
from = [1, 2, 3....20]
s.pack(*dist, *from)
print s #this would be binary representation of your C structure

Obviously, you can unpack back it using s.unpack method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.