Creating a list of methods to be executed in Python - Stack Overflow
How do I mock boto3 method calls when they're called from within a custom class' custom method?
List of all Python dunder methods?
Sounds like a classic case of X-Y-problem. What are you trying to achieve?
I don't think trying to maintain a comprehensive list of dunder methods is a good idea. Consider that this list is bound to change with upcoming versions of Python and has already undergone considerable changes in the past. You may end up maintaining varying lists of magic methods for different versions of Python.
I think the goto approach here would be to just try to call the respective method from within a generic __getattribute__ and react appropriately to TypeError.
It may also be possible to analyse the proxied object's class dict (someobject.__class__.__dict__) to find out which dunder methods were defined on that class. Of course you may end up having to walk up the inheritance chain as well, so this is bound to get quite complicated.
What is Python's list.append() method WORST Time Complexity? It can't be O(1), right?
Videos
You could create a list of the method names then use getattr() to access the methods:
instructions = ["shrink", "grow", "shrink"]
for i in instructions:
getattr(elephant, i)()
As an alternative to using strings for your list of instructions, you could do the following:
instructions = [someClass.shrink, someClass.grow, someClass.shrink,
someClass.shrink, someClass.grow, someClass.invert]
elephant = someClass(90)
sizeList = []
for ins in instructions:
ins(elephant)
sizeList.append(elephant.size)