getattr(...)
will get a named attribute from an object; getattr(x, 'y')
is equivalent to x.y
.
Where as d.method(*n)
will try to lookup the method named method
in deque
object result in AttributeError: 'collections.deque' object has no attribute 'method'
>>> from collections import deque
>>> d = deque()
>>> dir(d) # removed dunder methods for readability
[
"append",
"appendleft",
"clear",
"copy",
"count",
"extend",
"extendleft",
"index",
"insert",
"maxlen",
"pop",
"popleft",
"remove",
"reverse",
"rotate",
]
>>> method = "insert"
>>> d.method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'collections.deque' object has no attribute 'method'
>>> insert_method = getattr(d, method)
>>> insert_method
<built-in method insert of collections.deque object at 0x000001E5638AC0>
>>> help(insert_method)
Help on built-in function insert:
insert(...) method of collections.deque instance
D.insert(index, object) -- insert object before index
>>> insert_method(0, 1)
>>> d
deque([1])