existence of a variable
Stefan Schwarzer
s.schwarzer at ndh.net
Sat Jan 29 07:33:21 EST 2000
Hello Martin,
martindvorak at my-deja.com schrieb:
> What is the best way to determine existence
> of a variable in Python (I need to check
> if a variable is defined and if so,
> delete it)?
If you know in which module (class etc.) the variable would be,
you may use the builtin hasattr function:
[warpy|d:/]echo a = 7 >test.py
[warpy|d:/]python
Python 1.5.2 (#0, Jun 27 1999, 11:23:01) [VisualAge C/C++] on os2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import test
>>> hasattr( test, 'a' ), hasattr( test, 'b' )
(1, 0)
>>> class testclass:
... def f(): pass
...
>>> hasattr( testclass, 'f' ), hasattr( testclass, 'g' )
(1, 0)
<Disliking-the-inconsistency-of-hasattr-vs.-has_key>-ly y'rs
Stefan
More information about the Python-list
mailing list