调用成员函数并在python中使用map函数传递参数

时间:2012-10-10 09:59:57

标签: python matplotlib

根据我在this帖子中看到的内容,我尝试编写这段代码,但它给了我错误。

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

错误代码:

  

map(set_color,ticklabels)AttributeError:'Text'对象没有   属性'set_color(“b”)'

你不能将参数传递给methodcaller中的函数吗?

1 个答案:

答案 0 :(得分:2)

我认为你需要:

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

第一个参数是要调用的方法,后续参数将在调用时传递给方法。

然后您可以通过执行以下操作来测试它:

set_color(ticklabels[0])