python - 告诉 IPython 交互小部件忽略 OOP 中的 self 参数
问题描述
我正在尝试将 IPython 交互小部件用于我的一个类中的特定方法。我希望它忽略self
&colour
参数,只调整i
参数,但它坚持认为这self
也是一个真正的参数。
这里问了一个类似的问题,但在这种情况下,这两种方法(使用fixed
on self
或预加载self
到方法中partial
)都不合适。
from ipywidgets.widgets import interact,fixed
import matplotlib.pyplot as plt
class Profile():
'''self.stages is a list of lists'''
@interact(i=(0,5,1),colour=fixed(DEFAULT_COLOR))
def plot_stages(self, i, colour):
plt.plot(self.stages[i], color=colour)
这将返回一个错误:
ValueError: cannot find widget or abbreviation for argument: 'self'
那么如何告诉interact
忽略这一self
论点呢?
解决方案
定义一个接受self
和 常量参数的“外部”函数。在里面定义了一个“内部”函数,它只接受交互式参数作为参数,并且可以self
从外部范围访问常量参数。
from ipywidgets.widgets import interact
import matplotlib.pyplot as plt
DEFAULT_COLOR = 'r'
class Profile():
'''self.stages is a list of lists'''
stages = [[-1, 1, -1], [1, 2, 4], [1, 10, 100], [0, 1, 0], [1, 1, 1]]
def plot_stages(self, colour):
def _plot_stages(i):
plt.plot(self.stages[i], color=colour)
interact(_plot_stages, i=(0, 4, 1))
p = Profile()
p.plot_stages(DEFAULT_COLOR)
推荐阅读
- xamarin.forms - 如何在 UI 测试中使用不同的代码
- amazon-web-services - 在 aws ec2 实例上设置 kubernetes 集群时,salt-master not working yet 错误
- docker - Docker Compose:使用 mysql:8 的 SpringBoot 应用程序
- json - 根据Postgres中的整数值查询json列
- java - 无法在通过单击应用程序中的链接打开的浏览器中找到元素
- node.js - sequelize datatypes.NOW 函数不生成时间戳
- eclipse - Eclipse“保存服务器项信息”
- c# - 使用SqlKata在sql中按月份名称分组
- apache-kafka - KAFKA 分区数据实现每日数据
- r - glm - R 中的异常值检测和删除