0

I having difficulties to understand the instance of an object in a list. How to save the value of an object into a list without saving the instance? This is not possible isnt it? The colde below works but i would like to avoid to use .value as i might have several parameters.. not sure if i am clear enough..

class BougieBuffer:
    def __init__(self):
        self.bougiebuffer=deque(maxlen = 10000)
        self.maximum = Maximum()

    def update(self,bougie):
        self.maximum.value = random.randint(-1,1)
        bougie.maximum.value = self.maximum.value
        self.bougiebuffer.append(bougie)
        print len(self.bougiebuffer)
        for i in range (len(self.bougiebuffer),0,-1):
            print self.bougiebuffer[i-1].prixFermeture,   self.bougiebuffer[i-1].maximum.value 

I would have wrote naturally something like below but obviously this is not working and it returns the same value for all

bougie.maximum = self.maximum
5
  • The value of of an object? An instance is an object, it's class is an object, the instance attributes are all objects. What are you trying to achieve here? Commented Apr 18, 2013 at 10:11
  • Hi Martin, I am trying to save in bougie.maximum the self.maximum, however the class maximum contains several attributes (ie value, time, etc) i dont want to have to write bougie.maximum.value = self.maximum.value and repeat this for all different attributes of the class so i need to write bougie.maximum = self.maximum to "save" everything in one line, however it didnt work and it "saved" only the last instance for all. Commented Apr 18, 2013 at 10:24
  • I found out though at the meantime if i am "resetting" the instance and use self.maximum = Maximum() at the end of the method update it does the expected results : every items in bougiebuffer carries their own "maximum" value. Could you please explain with your words why do we need to write self.maximum = Maximum() at the end of update? Commented Apr 18, 2013 at 10:24
  • So you want to create a copy of the Maximum() instance to assign to the other class. Commented Apr 18, 2013 at 10:25
  • thats right, but i couldn't find a way to have this copy "independant" and not getting changed anymore if it makes sense... Commented Apr 18, 2013 at 10:27

1 Answer 1

1

You want to create a copy of the Maximum() instance to assign to the bougie.maximum attribute; use either copy.copy or copy.deepcopy:

from copy import deepcopy

bougie.maximum = deepcopy(self.maximum)

You'll need deepcopy if there are any attributes of Maximum that are mutable; a list, dict, set or another custom class instance are all mutable, but things like integers and strings are not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.