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

Viewed 78

I am new to Perl and I'm having trouble understanding this code. What does the if condition check here?

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

The condition is a list assignment. if enforces boolean, i.e. scalar context; arrays in scalar context return their size. Therefore, the condition is true if the array is populated, false if it's empty.

The method fetchrow_array most probably comes from 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.

Related