Python 中是否有类似于 Perl 中的 "want" 的内容
Is there something in Python that is similar to "want" in Perl
在 Perl 中,有一种方法可以找出调用函数的上下文 - 无论是列表上下文还是标量上下文 - 甚至更细粒度。参见 wantarray and the want module。
Python中有类似的东西吗?
不,Python 没有 Perl 所具有的 scalar/array 区别。值只是绑定到名称,而不考虑值的类型。
我能想到的最接近的类比是
x += y
这被脱糖为 x.__iadd__(y)
,因此方法 __iadd__
可以根据需要检查其参数 y
的类型。例如,即使 list.__iadd__
不会 这样做,理论上它也可以定义为允许
x = []
x += 9 # x == [9]; append a non-iterable argument
x += [11, 12] # x == [9, 11, 12]; extend with an iterable argument
在 Perl 中,有一种方法可以找出调用函数的上下文 - 无论是列表上下文还是标量上下文 - 甚至更细粒度。参见 wantarray and the want module。
Python中有类似的东西吗?
不,Python 没有 Perl 所具有的 scalar/array 区别。值只是绑定到名称,而不考虑值的类型。
我能想到的最接近的类比是
x += y
这被脱糖为 x.__iadd__(y)
,因此方法 __iadd__
可以根据需要检查其参数 y
的类型。例如,即使 list.__iadd__
不会 这样做,理论上它也可以定义为允许
x = []
x += 9 # x == [9]; append a non-iterable argument
x += [11, 12] # x == [9, 11, 12]; extend with an iterable argument