c to python
Mel Wilson
mwilson at the-wire.com
Mon May 26 17:03:25 EDT 2003
In article <mailman.1053970727.15498.python-list at python.org>,
"Jimmy verma" <jim_938 at hotmail.com> wrote:
>Hi !
>
>I have a structure in c like this
>
>struct ab
>{
> int a;
> int b;
> int *c;
> struct d *d;
>} AB;
>
>
>And i am using it in my program like
>
>void XYZ(int a , AB *b)
>
>
>How can this kind of structure be translated in python code?
>
>Your suggestions are welcomed.
(I'm confused.. did you leave out a typedef there?)
My first thought is:
class Struct: "a kludgy simulation of C structs"
some_struct = Struct() # to use later
a_shareable_int = [34] # or some other value
my_struct = Struct()
my_struct.a = 0 # or some other int value as required
my_struct.b = 17 # or some other int value as required
my_struct.c = a_shareable_int # kludge here
my_struct.d = some_struct # no kludge here
#
# ...
#
XYZ (112, my_struct)
The kludge concerning my_struct.c is that if you want to
share a "variable" with some other process, so that both
this and the other can make and see changes, you have to
share a mutable object. Here, I've used a list, but a
dictionary or a class instance would also do. All the code
that uses `a_shareable_int` as an integer has to refer to
a_shareable_int[0] or my_struct.c[0], and so forth. (as,
indeed, the C version could say *my_struct.c or
my_struct.c[0] .. cute coincidence.) Assigning directly to
my_struct.c would be pretty much equivalent to changing the
address in the pointer in the C version.
There's no kludge regarding my_struct.d, because its
object is a class instance, and that's mutable anyway.
Meaning that if some code in the program did
some_struct.m1 = 576
then any code that then looked at my_struct.d.m1 would see
576.
Regards. Mel.
More information about the Python-list
mailing list