469,609 Members | 1,455 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

checking whether a var is empty or not

Are these equivelent? Is one approach prefered
over the other

#check to see if var contains something... if so
proceed.
if var is not None:
continue

#check to see if var is empty... if so prompt user
again.
if not var:
print "Please specify the amount."
...
Jul 18 '05 #1
9 2474
At some point, Bart Nessux <ba*********@hotmail.com> wrote:
Are these equivelent? Is one approach prefered over the other

#check to see if var contains something... if so proceed.
if var is not None:
continue

#check to see if var is empty... if so prompt user again.
if not var:
print "Please specify the amount."
...


They're not equivalent: if var is None, the first doesn't trigger, but
the second does.

Do you mean:

if var is None:
# do something
if not var:
# do something

The first if only triggers if var is the singleton None.
The second if will trigger if var is False, 0, 0.0, 0j, '', [], (), None,
or anything else that has a length of 0 or var.__nonzero__() returns
False. In this case, you're checking if var is false in a boolean
logic context.

If you're checking arguments to a function to see if a non-default
argument has been passed, you probably want the first, like this:

def function(var=None):
if var is None:
# do something to get a default argument

But if you want a flag that's true or false, you want the second:
def function(var):
if var:
# yes, do something
else:
# no, do something else

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #2
Bart Nessux wrote:
Are these equivelent? Is one approach prefered over the other

#check to see if var contains something... if so proceed.
if var is not None:
continue

#check to see if var is empty... if so prompt user again.
if not var:
print "Please specify the amount."
...

They're not quite equivalent. The second form ('if not var') will
resolve to be true if var is any value that resolves to false -- this
could be None, 0, [], {}, '', or some user-defined objects. The first
form will only be true if var is None.

This could be significant if, for instance, 0 is a valid value. You
might want to initialize var to None, conditionally assign an integer to
it, and then later see if an integer (including 0) was actually
assigned. In that case, you'd need to use the first form.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #3


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.

Are these equivelent? Is one approach prefered over the other

#check to see if var contains something... if so proceed.
if var is not None:
continue

#check to see if var is empty... if so prompt user again.
if not var:
print "Please specify the amount."
...


Jul 18 '05 #4
In article <opsbxubr1g1v4ijd@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)).

--
.:[ 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
Jul 18 '05 #5
Dave Benjamin wrote:
In article <opsbxubr1g1v4ijd@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
Jul 18 '05 #6
In article <ap*******************@nasal.pacific.net.au>, Dave Cole wrote:
Dave Benjamin 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)).

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 :-).


Not that there's *any* reason to do anything like this, *ever* ;) but...
import inspect
def isset(v): .... return v in globals() or v in inspect.currentframe().f_back.f_locals
.... isset('a') False a = 5
isset('a') True def f(): .... b = 6
.... print isset('b')
.... print isset('c')
.... f() True
False c = 42
f()

True
True

Verdict: Just catch the NameError, already! =)

--
.:[ 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
Jul 18 '05 #7
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 ?

Jul 18 '05 #8

import inspect
def isset(v):
return v in globals() or v in inspect.currentframe().f_back.f_locals

Why would you want that ?

Use this PHP emulator :

from (all installed modules...) import *

If you want to emulate PHP, you should only use global variables, and
don't forget to copy those into all the modules you import (and of course,
update globals with all variables coming from all modules).
Jul 18 '05 #9
In article <opsb9nh9xd1v4ijd@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
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Philip D Heady | last post: by
3 posts views Thread by David P. Jessup | last post: by
14 posts views Thread by Christopher Benson-Manica | last post: by
2 posts views Thread by Marlene Stebbins | last post: by
7 posts views Thread by Rehceb Rotkiv | last post: by
3 posts views Thread by Nader | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.