简体   繁体   English

如果条件,与此 Perl 等效的 python 是什么?

[英]What would be the python equivalent of this Perl if condition?

I am new to Perl and I'm having trouble understanding this code.我是 Perl 的新手,我无法理解此代码。 What does the if condition check here? if 条件在这里检查什么?

my $ret = 0;
my @r = ();
if(@r = $sth->fetchrow_array()){
        $ret = $r[0];
}

The condition is a list assignment.条件是一个列表赋值。 if enforces boolean, ie scalar context; if强制执行 boolean,即标量上下文; arrays in scalar context return their size. arrays 在标量上下文中返回它们的大小。 Therefore, the condition is true if the array is populated, false if it's empty.因此,如果数组已填充,则条件为真,如果为空,则条件为假。

The method fetchrow_array most probably comes from DBI . fetchrow_array方法很可能来自DBI It returns a row from a database result.它从数据库结果中返回一行。 If there are no more rows or if there's an error, it doesn't return anything: that would make the condition false.如果没有更多行或有错误,它不会返回任何内容:这会使条件为假。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM