1

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...

3
  • 3
    Duplicate of this: stackoverflow.com/questions/7821423/… Commented Oct 2, 2012 at 21:34
  • you can also use someClass.__dict__[ins](elephant), otherwise getattr() is good. Commented Oct 2, 2012 at 21:46
  • @Ashwini Looking at the docs, .__dict__ is an implementation detail. Why would you ever use it instead of getattr() anyway? Commented Nov 2, 2022 at 22:44

4 Answers 4

6

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)()
Sign up to request clarification or add additional context in comments.

Comments

3

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)

1 Comment

+1, for showing that you can call a method as if it is a function by passing the instance as a parameter; in this case elephant instance will be passed as the self parameter to the method (function)
0

Possibly naïvely:

for ins in instructions:
  getattr(elephant, ins)()

Gotchas include that ins must be a string and that it's probably wise to validate both that ins is what you really want to call and that getattr(elephant, ins) is a callable.

Comments

0

You can use dir to get all property names of an object, and getattr to get a property value of an object. You may also want to not call any non-callable properties (such as 2 or "foo"):

for m in dir(elephant):
    if not m.startswith('_'):
        f = getattr(elephant, m)
        if callable(f): # Do not try to call non-function properties
            f()

1 Comment

You might want to integrate callable()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.