Newbie Variable Definition Question

Ivan Van Laningham ivanlan at callware.com
Tue Dec 14 11:15:42 EST 1999


Hi All--

"Andrew N. McGuire" wrote:
> 
> Hey all,
> 
>     I am a python newbie and am picking up on it very quickly.
> Python seems to be a remarkably simple language to learn, however
> I have one question..
> 
>    This is primarily oriented towards the *nix guys out there....
> In Bourne / Korn / Bash shells there is the syntax...
> 
>     if [ $variable = "" ]; then
>         commands
>     fi
> 
>    This tests if a variable is defined, and if not, execute commands.
> Also there is the following statement as well....
> 
>     variable=${variable:=value}
> 
>    This checks if variable is defined and if not, it assigns it a value
> of "value".
> I was wondering if there was a way to check if a variable is assigned by
> referencing its
> name in python, even if there is a chance that it is not defined....
> All
> my attempts lead to NameError, perhaps this can be used, but I have not
> figured
> out how to keep the script from stopping after the NameError error.
> 
> Any help is greatly appreciated, and thanks in advance.
> 
> Regards,
> 
> Andrew N. McGuire
> UNIX System Administrator
> 
> --
> http://www.python.org/mailman/listinfo/python-list


try:
    x=variable
except NameError:
    variable="default value"
    x=variable

print x

<ain't-python-grand>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
ivanlan at home.com
http://www.pauahtun.org
See also: 
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
Author:  Teach Yourself Python in 24 Hours
----------------------------------------------




More information about the Python-list mailing list