8
Today when I checked some code at the office, I found the following code. It shocked me.
class XXXX():
def __init__(self, k, v):
for i in range(len(k)):
setattr(self, k[i], v[i])
Then I found that most of the classes are written in the same way. That means all the classes are the same class,the only different is their name.
In this project setattr() is used to set attributes and getattr() is used to get attributes In profile log setattr was called 2700 times and getattr was called 3800 times. The time consume was 0.003sec and 0.005sec respectively (whole process: 0.069sec).
Though I do think setattr and getattr drag down the speed, I'm not sure if a rewrite of all the code would make it better.
Does obj.attribute = value run faster than setattr(obj,'attribute',value)?