假设我在 Python 中有这个简单的函数:
def f(gender, name):
if gender == 'male':
return ranking_male(name)
else:
return ranking_female(name)
wheregender
属于['male', 'female']
whilename
属于['Adam', 'John', 'Max', 'Frodo']
(if gender
is male
) 或['Mary', 'Sarah', 'Arwen']
(otherwise)。
我希望应用interact
fromipywidgets
到这个函数f
。通常一个人会做
from ipywidgets import interact
interact(f, gender = ('male', 'female'), name = ('Adam', 'John', 'Max', 'Frodo'))
问题是现在的允许值name
取决于为 选择的值gender
。
我试图在文档中找到它,但找不到。我认为唯一可能重要的是这用于设置特征更改的动态通知。
Parameters
----------
handler : callable
A callable that is called when a trait changes. Its
signature should be ``handler(change)``, where ``change```is a
dictionary. The change dictionary at least holds a 'type' key.
* ``type``: the type of notification.
Other keys may be passed depending on the value of 'type'. In the
case where type is 'change', we also have the following keys:
* ``owner`` : the HasTraits instance
* ``old`` : the old value of the modified trait attribute
* ``new`` : the new value of the modified trait attribute
* ``name`` : the name of the modified trait attribute.
names : list, str, All
If names is All, the handler will apply to all traits. If a list
of str, handler will apply to all names in the list. If a
str, the handler will apply just to that name.
type : str, All (default: 'change')
The type of notification to filter by. If equal to All, then all
notifications are passed to the observe handler.
但我不知道该怎么做,也不知道文档字符串在说什么。任何帮助深表感谢!