Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying __bool__ and __nonzero__ #3056

rinslow opened this issue Jul 28, 2019 · 2 comments


Copy link

commented Jul 28, 2019

Consider the following case

cdef class Foo(object):
    def __bool__(self):
        return True
    def __nonzero__(self):
        return self.__bool__()

This causes a RecursionError when calling bool(Foo())

I know the work around is specifying only __bool__ but it's still a bug IMHO


This comment has been minimized.

Copy link

commented Jul 29, 2019

Cython knows both what Python mode you're writing your code in and which version you're compiling for, so it can perform the correct name substitutions. Just use __bool__, it'll show up as the correct method name in both 2 and 3. It should probably be an error to specify both.


This comment has been minimized.

Copy link

commented Aug 15, 2019

PR welcome to issue a warning when users define both that suggests defining only __bool__().
(Actually, it could warn whenever someone defines __nonzero__() in an extension type.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.