checking whether a var is empty or not

Dave Cole djc at object-craft.com.au
Wed Aug 4 02:29:29 EDT 2004


Dave Benjamin wrote:
> In article <opsbxubr1g1v4ijd at musicbox>, Pierre-Frédéric Caillaud wrote:
> 
>>	This smells like PHP to me...
>>
>>if var is not None:
>>	if var has not been assigned, it raises an exception.
>>	if var has been assigned, it contains a value which can be None or  
>>someting else.
>>
>>	This is different from PHP where you can't know if a variable exists or  
>>not, because a non-existent variable will contain null if you check it,  
>>and putting null in a variable is like deleting it, but noone knows  
>>because there's no way of checking if a variable really exists, etc.
> 
> 
> 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)).

 >>> def isset(var):
...     return var in globals()
...
 >>> print isset('var')
0
 >>> var = 43
 >>> print isset('var')
1

Not that I can see any good use for this :-).

- Dave

-- 
http://www.object-craft.com.au



More information about the Python-list mailing list