[英]call member functions and pass arguments with map function in python
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")'
map(set_color,ticklabels)AttributeError:'Text'对象没有属性'set_color(“b”)'
Can't you pass argument to the function in methodcaller? 你不能将参数传递给methodcaller中的函数吗?
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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.