__nonzero__() appears FUBARed

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

__nonzero__() appears FUBARed

Leo User
Hello folks,

I was running test_bool.py from python 2.5 against
jythonx and ran into this unappealing error:
class a(object):
   def __nonzero__(self):
       return self

b = a()
print b.__nonzero__()
if b: print b

The problem in this case is the PyObjectDerived looks
for __nonzero__, if it finds it, it is executed.  Then
the returned value has __nonzero__ called on it.  In
this case, the code ends up in a recursive nightmare.
Im going follow the advice of a jythonxer to let this
sit for awhile since the initial solution/reaction
does not look pretty or minor.  Hopefully something
simple will suggest itself.  

I think the question somewhat hinges on when
__nonzero__ is called.  As we can see calling user
defined method is fine but put it in a situation where
the call is directly against the __nonzero__ method,
things go haywire.  If its isolated to a few
situations where a boolean return value will suffice,
it may end up being cheap.

This also raises the question of how many equivilent
methods exist, methods that are not __nonzero__ but
calculate return values based off of something
returned from a user defined method.  An entirely new
may be needed to deal with the "general" problem.

anyway, it doesn't look pleasant,

Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.

Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
Jython-dev mailing list
[hidden email]