Shadow Detection?

Gary Herron gherron at islandtraining.com
Tue May 9 16:45:06 EDT 2006


Michael Yanowitz wrote:

>Hello:
>
>  Many times, people are warning things like
>"Don't use 'str' as a variable name as it will shadow the
>built in str function." 
>   Is there some way to determine if a string is already
>defined in some higher scope?
>Maybe something like
><code>
>
>if isdefined ('str'):
>   print 'str is already defined, please choose another name'
>
></code>
>
>  If not, would it be easy to write this?
>
>Thanks in advance:
>
>  
>

Just try to access the name and see what happens, catching the resulting 
error if you wish

try:
   xyzzy
except NameError:
  print 'xyzzy is not currently bound'





More information about the Python-list mailing list