Massive assignment for many instances in python
Suppose I have a class:
class Foo:
def __init__(self):
self.prop = None
I need to do:
[foo1.prop, foo2.prop, ..., fooN.prop] = [prop_value1, prop_value2, ..., prop_value3]
Obviously it's N
always the same, so I have to do it dynamically. The first approach is to do a for loop:
for foo, value in zip(foo_list, value_list):
foo.prop = value
But the question is ... is there a better approach in Python? I mean better performance.
By the way, N > 1000
.
source to share
"Is there a better approach in Python? I mean better performance."
I do not believe there is. Let me do some temporary experiments. First, consider an alternative way of doing your job: you can use a setter for your class and call that setter inside an understanding / map.
class Foo:
def __init__(self):
self.prop = None
def setProp(self, prop):
self.prop = prop
Now you can do this:
from functools import partial f = partial(Foo.setProp) fooList = [...] propList = [...] list(map(f, fooList, propList))
This works as expected. We can use timeit
to get some temporary results with N = 1000
:
- For loop approach
10,000 loops, best of 3: 95.8 ΞΌs per loop
- Map approach
1000 loops, best of 3: 199 ΞΌs per loop
Conclusion: It seems that the timings differ by 2 times. I believe that nothing can beat the for loop for this particular use case, it is the syntax of the language and is faster than using any function.
source to share
One way to improve might be that you store the class variables already in a python compatible dict (think pickle). Then you can do something similar to
Foo.__dict__ = loaded_dict_value
This may be faster than the suggested method due to the fact that
Foo.value = loaded_value
Equivalent Foo.__dict__['value'] = loaded_value
. This means one more lookup (for the memory location "value" in __dict__. However, I don't suspect you'll get much performance out of this.
source to share