我在我的 kv 文件中定义了一个下拉小部件,例如
<CustomDropDown>:
myadd_curve_button: add_curve_button
MyButton:
id: add_curve_button
text: 'Add Curve'
听说kivy会把kv文件中定义的
Dropdown
的孩子放入Dropdown
的容器中。在我的应用程序中,我想遍历 Dropdown
容器中的子项,并能够捕获 kv 文件中定义的子项。但是 is
语句和内存位置让我感到困惑:
def reset_children(self):
print(f'button address with id(): {id(self.myadd_curve_button):x}')
print(f'child address with id:() {id(self.container.children[0]):x}')
print(f'self.myadd_curve_button: {self.myadd_curve_button}')
for child in self.container.children:
print(f'child: {child}')
if not( child is self.myadd_curve_button):
self.remove_widget(child)
del child
其中
reset_children()
是python中定义的CustomDropDown
类的一个方法。令人困惑的部分是输出:
button address with id(): 7f9bd5b62bd0
child address with id:() 7f9bd5b5db50
self.myadd_curve_button: <__main__.MyButton object at 0x7f9bd5b5db50>
child: <__main__.MyButton object at 0x7f9bd5b5db50>
为什么当我简单地打印对象时十六进制数相同,但调用
id()
时却不同? is
声明怎么会返回False
?
我已经尝试打印地址来推断为什么
is
比较返回 False
。我被告知 __str__()
的默认实现应该显示与 id()
返回的地址相同的十六进制地址。