[IronPython] How do I test if a variable is defined?

Michael Foord fuzzyman at voidspace.org.uk
Wed Nov 5 11:07:51 CET 2008


Tony Meyer wrote:
> On Wed, Nov 5, 2008 at 8:06 AM, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
>   
>> Marty Nelson wrote:
>>     
>>> How do I test in Iron Python (in python) if a variable is defined?
>>>
>>>       
> [...]
>   
>> try:
>>   name
>> except NameError:
>>   # variable 'name' is not defined
>>     
>
> If you're running some sort of checker (pylint, pychecker) over this,
> you might get a warning about the "name" line not doing anything.  To
> avoid that, you'll sometimes see a variant like this:
>
> try:
>     unused = name
> except NameError:
>     # variable 'name' is not defined.
>   

Except then PyLint will complain that 'unused' is unused, plus you code 
no longer accurately conveys its intent.

I prefer to filter (or disable) that particular warning rather than 
change the code.

Michael

> Cheers,
> Tony
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/




More information about the Ironpython-users mailing list