function to check whether a variable already exists?
Roman Suzi
rnd at onego.ru
Tue May 1 13:20:08 EDT 2001
On Tue, 1 May 2001, Graham Guttocks wrote:
>Can anyone tell me how to wrap the following in a function as to
>avoid typing this try, except sequence for every variable whose
>existence I want to check?
>
>For example,
>
>try:
> if NEWSRC:pass
>except NameError:
> NEWSRC = os.path.expanduser("~/.newsrc")
try: NEWSRC
except: NEWSRC = ...
or:
if not vars().has_key("NEWSRC"): NEWSRC = ...
or:
myvars = [('NEWSRC', """os.path.expanduser("...")"""),
...
]
for (v, default) in myvars:
try: eval(v)
except: exec v + " = " + default
-- really bad solution, speaking of bad design.
It will be interesting to hear others.
>It seems tricky, because you can't just pass a function NEWSRC because
>if it doesn't first exist NameError will immediately be raised.
>
>Regards,
>Graham
>
>__________________________________________________
>Do You Yahoo!?
>Yahoo! Auctions - buy the things you want at great prices
>http://auctions.yahoo.com/
>
>
Sincerely yours, Roman Suzi
--
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Tuesday, May 01, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "Caterpillar: Scratching post." _/
More information about the Python-list
mailing list