checking whether a var is empty or not

Dave Benjamin ramen at lackingtalent.com
Fri Aug 6 18:15:01 CEST 2004


In article <opsb9nh9xd1v4ijd at musicbox>, Pierre-Frédéric Caillaud wrote:
> 
>> No, in PHP, you can find out if a variable exists using isset(). And  
>> trying
>> to dereference an uninitialized variable will generate a warning if you  
>> have
>> error reporting turned up all the way (error_reporting(E_ALL)).
> 
> 	WRONG !!!!!
> 	Example in this stupid language :
> 
><?php
> 
> 	echo "<p>one : ";
> 	var_dump(isset( $a ));
> 	
> 	$a = 1;
> 	echo "<p>two : ";
> 	var_dump(isset( $a ));
> 	
> 	$a = null;
> 	echo "<p>three : ";
> 	var_dump(isset( $a ));
> 
> ?>
> 
> 	Output :
> 
> one : bool(false)
> two : bool(true)
> three : bool(false)
> 
> 	Get it ?

I stand corrected. That is rather stupid.

Well, I try to use nulls sparingly and always initialize my variables, which
may explain why in the five or so years I've been using PHP, I've never run
into this problem. Likewise, I don't think I've ever had to use the
corresponding idiom in Python:

try:
    a
except NameError:
    # ...

At the very least, I'll be testing for a's existence in some namespace, so
I'll be looking for an AttributeError.

-- 
  .:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.

"When the country is confused and in chaos, information scientists appear."
Librarian's Lao Tzu: http://www.geocities.com/onelibrarian.geo/lao_tzu.html



More information about the Python-list mailing list