How can I create a list of methods in python to be applied to an object?
Given some arbitrary class:
class someClass:
def __init__(self, s):
self.size = s
def shrink(self):
self.size -= 1
def grow(self):
self.size += 1
def invert(self):
self.size = -self.size
I want to be able to write an iterable object like this list:
instructions = [shrink, grow, shrink, shrink, grow, invert]
To be run through a for-loop later:
elephant = someClass(90)
sizeList = []
for ins in instructions:
elephant.ins()
sizeList.append(elephant.size)
I've done something similar with functions before. Not being able to do this with methods would require me to rewrite an intimidating amount of code...
someClass.__dict__[ins](elephant), otherwisegetattr()is good..__dict__is an implementation detail. Why would you ever use it instead ofgetattr()anyway?