Linked Questions
26 questions linked to/from C-like structures in Python
28
votes
4
answers
102k
views
how to define a structure like in C [duplicate]
I am going to define a structure and pass it into a function:
In C:
struct stru {
int a;
int b;
};
s = new stru()
s->a = 10;
func_a(s);
How this can be done in Python?
8
votes
1
answer
34k
views
C struct python equivalent [duplicate]
I have this C code:
typedef struct test * Test;
struct test {
void *a;
Test next;
};
How would you implement the equivalent to this in Python (if that is even possible)?
0
votes
2
answers
729
views
Does Python have a data structure sort of in-between dict and namedtuple? [duplicate]
I am looking for fixed keys, like a namedtuple, but mutable values like a dict. I would probably have used a struct in C. Is there such a data structure in Python?
3
votes
0
answers
369
views
equivalent of bitfield for C structure in python [duplicate]
I am having a bitfield structure in C and i want to implement the same in python.
With the structure i am having in C i am able to get 2 byte of data through it..and i write this data in a file in ...
1
vote
0
answers
86
views
How to implement structure data type in python [duplicate]
Could any one help me to implement C structure data type in python?
I need a similar structure data type in python as below.
struct msgbuf
{
long mtype;
char mtext[100];
};
1305
votes
14
answers
442k
views
Usage of __slots__?
What is the purpose of __slots__ in Python — especially with respect to when I would want to use it, and when not?
437
votes
34
answers
388k
views
Accessing dict keys like an attribute?
I find it more convenient to access dict keys as obj.foo instead of obj['foo'], so I wrote this snippet:
class AttributeDict(dict):
def __getattr__(self, attr):
return self[attr]
def ...
19
votes
9
answers
40k
views
struct objects in python
I wanted to create a throwaway "struct" object to keep various status flags. My first approach was this (javascript style)
>>> status = object()
>>> status.foo = 3
Traceback (most ...
9
votes
8
answers
1k
views
C++ Structure within itself?
I've been trying to port this code to python, but there is something I do not quite understand in C++ (I do know a bit of C++ but this is beyond me):
typedef struct huffnode_s
{
struct huffnode_s ...
4
votes
2
answers
4k
views
Manually pass commands into argparse? | Python 2.7
I am making a terminal game using Python's wonderful Cmd library. But i was curious if i could somehow put argparse code into it. Like use argparse to handle the 'args' from my cmd.Cmd() class.
To do ...
3
votes
4
answers
2k
views
Dictionary of (named) tuples in Python and speed/RAM performance
I'm creating a dictionary d of one million of items which are tuples, and ideally I'd like to access them with:
d[1634].id # or d[1634]['id']
d[1634].name # or d[1634]['name']
d[1634]....
2
votes
4
answers
1k
views
What is the proper way to bundle variables in python
I have three variables that are closely tied together and I do not want to pass separately every time I call a function. What is the proper way to bundle them.
Context: The purpose of the variables ...
-2
votes
2
answers
5k
views
Creating a struct in python
Currently I am porting a pascal program to python, and there I have the following code:
ScanList = Record
name : string;
I : Integer;
...
-3
votes
1
answer
2k
views
How to implement c++ type structures in python? [closed]
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 ...
3
votes
3
answers
697
views
Can I use a class variable in python similarly to how a struct variable in C is used?
I wrote a program in C that uses structs to model the motion of planets going around the Sun. I want to write that same program in Python. The planets and Sun are treated as particles.
In C I wrote ...