- 论坛徽章:
- 0
|
再继续...
打算在学习Perl的过程中持续更新这份帖子;有兴趣”顺便学习一下Python与shell”的人欢迎关注
(弱弱的说一声:其实我是来忽悠大家学习Python的 )
****
Perl中的list赋值与Python的tuple类似:
Perl/Python: ($fred, $barney, $dino) = ("flintstone", "rubble", undef);
Perl/Python: ($fred, $barney) = ($barney, $fred);
但是当两边的数目不等时, Perl默认处理,Python报错:
Perl:
($fred, $barney) = qw< flintstone rubble slate granite >; # two ignored items
($wilma, $dino) = "hello"; # $dino gets undef
Python:
>>> (a, b) = 'hello'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> (a, b) = ('hello', 'world', '!')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>>
****
Perl中表示list的括号似乎不能省略?有如下代码:
$a, $b = qw/ hello my god /; 则 a 的值为hello, b的值为god, 不解...
Python中通常可以省略该括号,故以下四个表达式等价:
(a, b) = 'hello', 'world'
a, b = 'hello', 'world'
(a, b) = ('hello', 'world')
a, b = ('hello', 'world')
****
[ 本帖最后由 chunchengfh 于 2009-1-12 16:05 编辑 ] |
|