1

in my Python code I have the following issue: i have to copy the same object many times and then pass each copy to a function that modifies it. I tried with copy.deepcopy, but it's really computationally expensive, then i tried with itertools.repeat(), but it was a bad idea because after that i've to modify the object. So i wrote a simple method that copy an object simply returning a new object with the same attributes:

def myCopy(myObj):
    return MyClass(myObj.x, myObj.y)

The problem is that this is really unefficient too: i've to make it abaout 6000 times and it takes more than 10 seconds! So, does exist a better way to do that?

The object to copy and modify is table, that is created like that:

def initialState(self):
    table = []
    [table.append(Events()) for _ in xrange(self.numSlots)] 
    for ei in xrange(self.numEvents - 1):
        ei += 1
        enr = self.exams[ei]
        k = random.randint(0, self.numSlots - 1)
        table[k].Insert(ei, enr)
    x = EtState(table)
    return x

class Event:

    def __init__(self, i, enrollment, contribution = None):
        self.ei = i
        self.enrollment = enrollment
        self.contribution = contribution



class Events:

    def __init__(self):
        self.count = 0
        self.EventList = []

    def getEvent(self, i):
        return self.EventList[i].ei


    def getEnrollment(self, i):
        return self.EventList[i].enrollment

    def Insert(self, ei, enroll = 1, contribution = None):
        self.EventList.append(Event(ei, enroll, contribution))
        self.count += 1

    def eventIn(self, ei):
        for x in xrange(self.count):
            if(self.EventList[x].ei == ei):
                self.EventList[x].enrollment += 1
                return True
        return False
glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Is the copy always modified or is it occasional modification you're protecting against? – Joachim Isaksson May 23 '13 at 09:05
  • 1
    What is the object you are copying? Show some code. – piokuc May 23 '13 at 09:05
  • couldn't you just pass the initial object to the modifying function and append the result to a list? You would end up with a list containing all the objects. – pypat May 23 '13 at 09:06
  • The copy is always modified. –  May 23 '13 at 09:22
  • I am sorry to say, but your code doesn't really make much sense. Maybe you should start by explaining what you are trying to achieve... – root May 23 '13 at 09:56
  • I can imagine, but i posted only the structure of what i've to copy, not how i do it nor what other methods do with it. My initial question was very general. In the method i posted there are only the way of how the object is created and the particular classes i used in that. –  May 23 '13 at 10:16
  • Why can't you just do `my_modifying_func(obj=MyClass(args))`? As for what you actually asked, have you considered the possibility that you are doing the wrong way around? – root May 23 '13 at 10:49

1 Answers1

0

More Pythonic way would be to create function(s) that modify the object, but don't modify the original object, just return its modified form. But from this code you posted, it is not clear what are you acutally trying to do, you should make a more simple (generic) example of what are you trying to do.

Since Object in Python means anything, class, instance, dict, list, tuple, 'a', etc..

to copy object is kind of not clear... You mean copy instance of a Class if I understood it correctly

So write a function that takes one instance of that class, in that function create another instance and copy all atributes you need..

Lu.nemec
  • 484
  • 6
  • 10