Commit 44c94328 by Marius Gedminas

Speed up execution

`if method in dir(self):` is very inefficient:

- it must construct a list object listing all the object attributes & methods
- it must then perform a O(N) linear scan of that list

Replace it with the idiomatic `if hasattr(self, method):`, which is a
O(1) expected time hash lookup.

Should fix #11981.
parent efa005c2
...@@ -96,7 +96,7 @@ class Base: ...@@ -96,7 +96,7 @@ class Base:
@staticmethod @staticmethod
def _generic_g(prop_name, self): def _generic_g(prop_name, self):
method = "_get_attr_%s" % prop_name method = "_get_attr_%s" % prop_name
if method in dir(self): if hasattr(self, method):
return getattr(self, method)() return getattr(self, method)()
return self._attributes[prop_name] return self._attributes[prop_name]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment