[英]is it possible to iterate a Tuple traitlets?
I transformed some of my lib variable into traitlets
and I'm facing an error when iterating on a Tuple
even though this is possible with built-in tuple
.我将我的一些 lib 变量转换为
traitlets
,并且在迭代Tuple
时遇到错误,即使这可以通过内置tuple
实现。
from traitlets import Tuple
a = Tuple((1,2,4)).tag(sync=True)
for i in a:
print(i)
>>> ---------------------------------------------------------------------------
>>> TypeError Traceback (most recent call last)
>>> /Users/pierrickrambaud/Documents/travail/FAO/app_buffer/sepal_ui/untitled.ipynb >>> Cellule 14 in <cell line: 1>()
>>> ----> 1 for i in a:
>>> 2 print(i)
>>>
>>> TypeError: 'Tuple' object is not iterable
with the normal tuple
:使用普通
tuple
:
a = (1, 2, 3)
for i in a:
print(i)
>>> 1
>>> 2
>>> 3
Is it a bug or is it the intended behavior?它是错误还是预期的行为?
The answer is yes of course.答案当然是肯定的。
The issue I was getting in my application were due to a typo and the bug is only a side effect, For future reader the small example I gave is not showing the behaviour of a trait as it should be used.我在我的应用程序中遇到的问题是由于拼写错误,错误只是一个副作用,对于未来的读者,我给出的小例子没有显示应该使用的特征的行为。 Instead create the trait in a class extending
HasTraits
:而是在扩展
HasTraits
的 class 中创建特征:
from traitlets import Tuple, HasTraits
class A(HasTraits):
toto = Tuple((1,2,3))
a = A()
for i in a.toto:
print(i)
>>> 1
>>> 2
>>> 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.