c to python

Gerald Klix Gerald.Klix at klix.ch
Mon May 26 15:45:58 EDT 2003


Thats easy if you want your structure's members to be initialized on 
creation (with None)

class ab:
  def __init__( self ):
   self.a = None
   self.b = None
   self.c = None
   self.d = None

or shorter

  self.a = self.b = self.c = self.d = None

If you don't care about initialisation.
You can just define a class called MyStructureSurrogate
and create instances as you like.

class MyStructureSurrogate:
  pass

si = MyStructureSurrogate()
si.a = 1
si.b = 4711
si.d = MyStructureSurrogate()
si.d.a = 2

and so on

Because python has strict referrence semantics a actually get something
like this:

struct ab
{
       int *a;
       int *b;
       int *c;
       struct d *d;
} AB;

with pointers automaticly dereferenced.

HTH to start with python,
Gerald


Jimmy verma wrote:
> Thanks for responding. I dont want the python extension for my C code as 
> i have quite a big program in C.  I will prefer to write my program from 
> scratch in python. I will be thankful if you can tell me how can i write 
> an  equivalent code in python for this sort of example i have mentioned.
> 
> 
> Your link will be helpfull for me while writing my program in python. 
> Thanks for that.
> 
> Waiting for your response.
> 
> Regards,
> 
> 
>    From: Gerhard Häring <gh at ghaering.de>
> To: python-list at python.org
> Subject: Re: c to python
> Date: Mon, 26 May 2003 20:22:57 +0200
> 
> Jimmy verma 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?
> 
> As far as I see, you can't. Python doesn't know about pointers, even 
> with the struct module 
> (http://python.org/doc/current/lib/module-struct.html).
> 
>    Your suggestions are welcomed.
> 
> It might be that you'll have to write a C extension to wrap your 
> existing C code for use with Python.
> 
> If, however, you don't need any binary compatibility of your existing C 
> code and you simply want an equivalent data structure in Python, then 
> that's certainly easily doable.
> 
> We'd need to know why you have the 'int* c' in your C struct 
> declaration, then.
> 
> -- Gerhard
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> _________________________________________________________________
> Got a wish? Make it come true. 
> http://server1.msn.co.in/msnleads/citibankpersonalloan/index.asp Best 
> personal loans!
> 
> 





More information about the Python-list mailing list