1

I'm pretty new to Python and I'm having difficulty figuring out how to use the structs. What would the C structs below look like when they are converted into Python?

These are the structs I have:

struct dataT
{
    int m;
};

struct stack
{
    int top;
    struct dataT items[STACKSIZE];
} st;

How would this statement be represented in Python?

st.items[st.top].m
4
  • Python doesn't have structs. Commented Apr 1, 2015 at 18:38
  • @Kevin It basically does. A struct is just a class where all the members and methods are public instead of private by default. In fact in this regard Python's classes are closer to C's struct than class since there isn't a notion of private Commented Apr 1, 2015 at 18:44
  • @Cyber: You're conflating the languages. In Python, all attributes and methods are public by default (although its concept of public vs private are different from C++'s). C doesn't have classes, but C++ does, and in C++ a struct is simply a class where all the members and methods are public by default. In C, structs can't have methods because they're not classes since there's no such thing. C also doesn't have a concept of public and private. Commented Apr 1, 2015 at 19:25
  • @martineau My mistake, I meant to write C++ in my comment above. But you are correct in your explanation. Commented Apr 1, 2015 at 19:27

2 Answers 2

2

You just need to define your dataT class

class dataT():
    def __init__(self, m=0):
        self.m = m

You can instantiate one like

d = dataT(5)

The stack behavior you can get from the list class already

>>> l = [dataT(i) for i in range(5)]
>>> l.pop().m
4
>>> l.pop().m
3
>>> l.pop().m
2
>>> l.pop().m
1
>>> l.pop().m
0
>>> l.append(dataT(3))
>>> l.pop().m
3
Sign up to request clarification or add additional context in comments.

4 Comments

where is it mentioned that m is an integer? How does python know wether it is is an integer or not ?
@Benjer m will be whatever type you assign to it. I happened to put int values in there, but you can put anything. Python is not a statically-typed language.
Sorry, im pretty new to pyhton, so basically the strucutre doent have a max value assigned to it, and any element can be put into it? Is there a way to only allow integers?
Yes the list (analogous to your stack) is a Python sequence. Meaning it is sortable, indexable, resizeable, sliceable, etc. In that regard it is similar to a std::vector<T> in C++, but it is also heterogeneous. The way to limit the values to int would be to do some kind of validation using isinstance(m, int) or something like that.
0

You can use namedtuples to construct classes that can represent your structs:

from collections import namedtuple
dataT = namedtuple("dataT", ['m'])
stack = namedtuple("stack", ['top', 'items'])

st = stack(0, [])
st.items.append(dataT(5))
st.items[st.top].m

Though you will probably find that the stack class is unnecessary as pointed out list already has this behaviour.

4 Comments

A possibly very significant difference between C structs and Python namedtuples is that the latter are immutable.
C struct's are mutable too, e.g. st.top = 1 would update st in place.
Yes, C structs are mutable. namedtuples are not.
Ahh, misread your comment, though the list in the namedtuple is mutable.

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.