Mel wrote:
<snip>
This is an artifact of the interactive interpreter,

True.  You can avoid the artifact by wrapping the test in a function:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
class A():
...   def __repr__ (self):
...     return b
...
def is_defined (obj):
...   try:
...     obj
...   except NameError:
...     return False
...   return True
...
a = A()
if is_defined (a):
...   print "`a` is defined"
... else:
...   print "`a` is not defined"
... `a` is defined
if is_defined (b):
...   print "`b` is defined"
... else:
...   print "`b` is not defined"
... Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
At the cost of it not quite working when the function is called with an undefined name. I suppose the print statements could be crafted to make it look better.


        Mel.

No clue what you're trying to accomplish here. The exception inside the function is_defined() will never fire; the method always returns True.

You're getting an exception during the attempt to call such a function, and so of course there's no point in putting the if test around that either.

This may be the last time I try to read such complexity in an interactive transcript. It took a couple of tries before I got the indentation understood. I thought that is_defined() was intended to be a method inside the class.

DaveA

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to