Hi All,
I've got a problem with the python
class. I want to create a list of object of type obj() and link them such
that the next of one object is the next in its place order. I tried this
construction

class obj:
------def __init__(self,cargo=None,next=None):
---------------self.next = next
---------------self.cargo = cargo
class circular:
------def __init__(self,next=None):
---------------self.next = next
---------------self.quad = [obj(),obj(),obj(),obj()]
---------------for i in range(3):
                           self.quad[i].next = self.quad[i+1]
                           self.quad[3].next = self.quad[0]

when I run it,
>>>a = circular()
>>>a.quad=[obj(1),obj(2),obj(3),obj(4)]
>>>print a.quad[0].next.cargo
None

which is not the link that I think, rather I have to do this

class obj:
          def __init__(self,cargo=None,next=None):
                    self.next = next
                    self.cargo = cargo
class circular:
          def __init__(self,next=None):
                    self.next = next
                    self.quad = [obj(),obj(),obj(),obj()]
          def setlink(self):
                    for i in range(3):
                              self.quad[i].next = self.quad[i+1]
                    self.quad[3].next = self.quad[0]

>>>>a = circular()
>>>a.quad=[obj(1),obj(2),obj(3),obj(4)]
>>>a.setlink()
>>>print a.quad[0].next.cargo
2
which is not very practical since we have to set always the link, is there
any possibility of link them automaticaly?

regards
Faniry

Maybe if you just set it so that the function call to 'setlink' happens at the end of the '__init__' function. This will make the function call for 'setlink' happen automatically, but you will need to pass the cargo values to the class on initialization, otherwise it won't have the correct obj() instances in the list before linking.

class obj:
          def __init__(self,cargo=None,next=None):
                    self.next = next
                    self.cargo = cargo
class circular:
          # added 'values' list as a param, for the obj list.
          def __init__(self, values=[], next=None):
                    self.next = next
                    if len(values) == 0:  # if no values passed...
                              self.quad = [obj(),obj(),obj(),obj()]
                    else:  # set object list to values in list
                              for val in values:
                                        self.quad.append(obj(val))
                    self.setlink()  # call linking func
          def setlink(self):
                    for i in range(3):
                              self.quad[i].next = self.quad[i+1]
                    self.quad[3].next = self.quad[0]
>>>>a = circular([1, 2, 3, 4])
>>>print a.quad[0].next.cargo

If you ever change the values in the obj list, then you'd need to call the 'setlink' function manually again, but this should work for the purpose you illustrated in your post.
Hopefully that does the trick! :D

Hi,

This seems working when we set the variable of circular. But it does not when we insert the values of quad example

In [16]: a = circular()

In [17]: a.quad[0] = obj(1)

In [18]: a.quad[1] = obj(2)

In [19]: print a.quad[0].next.cargo
---------------------------------------------------------------------------
<type 'exceptions.AttributeError'> Traceback (most recent call last)

/var/autofs/misc/home/faniry/Mylife/Essay2009/<ipython console> in <module>()

<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'cargo'

The thing is that, setting every time the link is not very useful since in very complicated calculation, we will forget to set it.

Well I changed the code so that it makes the quad list full of blank objs by default, then updates it with the numbers from the values passed to it if possible. This is the result:

class obj:
          def __init__(self,cargo=None,next=None):
                    self.next = next
                    self.cargo = cargo
class circular:
          # added 'values' list as a param, for the obj list.
          def __init__(self, values=[], next=None):
                    self.next = next
                    self.quad = [obj(),obj(),obj(),obj()] # set blank first
                    if len(values) > 0:  # set object list to values in list
                              self.quad = [ ]
                              for val in values:
                                        self.quad.append(obj(val))
                    self.setlink()  # call linking func
          def setlink(self):
                    for i in range(3):
                              self.quad[i].next = self.quad[i+1]
                    self.quad[3].next = self.quad[0]
>>>a = circular([1, 2, 3, 4])
>>>print a.quad[0].next.cargo
2

It seems to be working for me. And what's this about forgetting to set the link each time? Because when you initialize the circular class, the link gets set without you having to call it...

Unless you mean that if you edit one of the quad values, that it doesn't update the variables in the other quads. In which case you could just write a function on the circular class that you call in order to assign a new value to an index of the quad, and then it afterwards updates the links on its own. So instead of using the regular assignment operator =, you could call the function with the new value passed instead.

Thanks, I get the idea,

class obj:
          def __init__(self,cargo=None,next=None):
                    self.next = next
                    self.cargo = cargo
class circular:
          def __init__(self,next=None):
                    self.next = next
                    self.quad = [obj(),obj(),obj(),obj()]
          def setlink(self):
                    for i in range(3):
                              self.quad[i].next = self.quad[i+1]
                    self.quad[3].next = self.quad[0]
	  def replace(self,index,new_obj):
	            self.quad[index] = new_obj
		    self.setlink()

which when I run gives
>>>a = circular()
>>>a.replace(1,obj(3))#change the value of a.quad[1]
>>>a.quad[0].next.cargo#test the next value
3
Regards
Faniry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.