0

Based on what I see in this post, I tried to write this piece of code but it gives me error.

ticklabels = ax.get_xticklabels()
set_color = operator.methodcaller('set_color("b")')
ticklabels[0].set_color('b') # this runs fine
map(set_color, ticklabels)   #error is here

Error code:

map(set_color, ticklabels) AttributeError: 'Text' object has no attribute 'set_color("b")'

Can't you pass argument to the function in methodcaller?

3
  • Does map(lambda x:x.set_color, ticklabels) work? Commented Oct 10, 2012 at 10:06
  • @halex, yes your code also works and it is mentioned in the referred post. I just did not want to use lambda. Thanks for mentioning that. Commented Oct 10, 2012 at 10:25
  • The lambda variant should be map(lambda x: x.set_color('b'), ticklabels) Commented Oct 11, 2012 at 0:37

1 Answer 1

2

I think you need:

set_color = operator.methodcaller('set_color', 'b')

The first argument is the method to be called, subsequent arguments will be passed to the method when it is called.

You can then test it works by doing:

set_color(ticklabels[0])
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, did not read the documentation for the methodcaller. It is mentioned there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.