checking whether a var is empty or not

B

Bart Nessux

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."
...
 
D

David M. Cooke

At some point said:
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
 
J

Jeff Shannon

Bart said:
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
 
?

=?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=

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.
 
D

Dave Benjamin

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

Dave Cole

Dave said:
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)).
.... return var in globals()
....1

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

- Dave
 
D

Dave Benjamin

... return var in globals()
...
1

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

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

Verdict: Just catch the NameError, already! =)
 
?

=?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=

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 ?
 
?

=?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=

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

Dave Benjamin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top