concept of creating structures in python

Joe Strout joe at
Fri Dec 12 16:37:15 CET 2008

On Dec 11, 2008, at 10:52 PM, navneet khanna wrote:

> I want to create a structure within a structure i.e. nested  
> structures in python.
> I tried with everything but its not working.
> my code is like this:
> class L(Structure):
>     def __init__(self,Name='ND',Addr=0,ds_obj = D()):

Change the default value of ds_obj here to None.  Otherwise, you will  
certainly confuse yourself (there would be just one default object  
shared among all instances).

>         self.Name = Name
>         self.Addr = Addr
>         self.ds_obj = ds_obj
> class D(Structure):
>     def  __init__(self,dataName='ND',index = 0,ele_obj=E()):
>         self.dataName = dataName
>         self.index = index
>         self.ele_obj = ele_obj

Same thing here with ele_obj -- have it default to None to save  
yourself grief.

Otherwise, these look fine.

> these are two structures. I want to refer D structure in L one and  
> use it. I want to access the value of D structure like L.D.index = 0.

But you didn't name it "D" -- if you have an L object, say "foo", then  
you'd get to it's D object as foo.ds_obj (since that's what you named  
it in L.__init__).  If you then wanted to get to that thing's E  
object, it'd be foo.ds_obj.ele_obj.

- Joe

More information about the Python-list mailing list