Skip to content

Commit

Permalink
Dyn methods: Use setattr
Browse files Browse the repository at this point in the history
Not seeing any customizations that necessitate the use of the magic
method `__setattr__()` here, so using `setattr()` should be equivalent.
[[1]] [[2]]

Timeit is reporting 20 to 80us reductions in object instantation times,
all 20% drops relative to pydot 1.4.1.

No other changes in external behavior are expected.

[1]: https://stackoverflow.com/questions/14756289/setattrobject-name-value-vs-object-setattr-name-value
[2]: https://stackoverflow.com/questions/7559170/whats-the-difference-between-setattr-and-object-setattr

(This commit is part of a series of changes in how the `get_*`,
`set_*`, `create_*` and `write_*` methods are dynamically created.)
  • Loading branch information
peternowee committed Oct 26, 2020
1 parent 3bd7797 commit b54b4bc
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pydot.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,14 @@ def create_attribute_methods(self, obj_attributes):

# Generate all the Setter methods.
#
self.__setattr__(
setattr(self,
'set_'+attr,
lambda x, a=attr :
self.obj_dict['attributes'].__setitem__(a, x) )

# Generate all the Getter methods.
#
self.__setattr__(
setattr(self,
'get_'+attr, lambda a=attr : self.__get_attribute__(a))


Expand Down Expand Up @@ -1723,7 +1723,7 @@ def new_method(
return self.create(
format=f, prog=prog, encoding=encoding)
name = 'create_{fmt}'.format(fmt=frmt)
self.__setattr__(name, new_method)
setattr(self, name, new_method)

for frmt in self.formats+['raw']:
def new_method(
Expand All @@ -1734,7 +1734,7 @@ def new_method(
path, format=f, prog=prog,
encoding=encoding)
name = 'write_{fmt}'.format(fmt=frmt)
self.__setattr__(name, new_method)
setattr(self, name, new_method)

def __getstate__(self):

Expand Down

0 comments on commit b54b4bc

Please sign in to comment.