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

why do boolean comparisons with Py object return Py object? #294

Closed
schlichtanders opened this issue Apr 15, 2023 · 5 comments
Closed

why do boolean comparisons with Py object return Py object? #294

schlichtanders opened this issue Apr 15, 2023 · 5 comments

Comments

@schlichtanders
Copy link
Contributor

schlichtanders commented Apr 15, 2023

First time I am trying to use pythoncall support for defining python classes. Apparently all input arguments as well as self attributes get wrapped in Py.

This is also truth for boolean comparisons like == or != Which return Py objects.

This is difficult as we cannot simply use standard comparisons in if else && or ||, but instead need to wrap everything in Bool(...) In order to get a Julia boolean

What is the purpose of this extra complication? Wouldn't it be much easier if such boolean operators return Julia Bool values?

@cjdoris
Copy link
Collaborator

cjdoris commented Apr 24, 2023

This is because Python boolean operators do not always return booleans. For example, comparing two numpy arrays will return an array of booleans.

Some options:

  • Call pytruth (or Bool) as you already do.
  • Call pyeq(Bool, x, y), which is equivalent to pytruth(x == y) (and similar for other comparisons).
  • Call isequal(x, y), which is equivalent to the above too.

@schlichtanders
Copy link
Contributor Author

Thank you for the explanation. Would it be possible to nevertheless return a Julia Bool if a standard Boolean was returned?
I think this would massively simplifiy writing Py classes from Julia (for the initially stated reasons)

@cjdoris
Copy link
Collaborator

cjdoris commented Apr 24, 2023

I'm not going to do that I'm afraid, as it introduces type instability which is not only bad for performance but also makes code harder to reason about. This is one of the major design differences between this and PyCall (which automatically converts everything).

@github-actions
Copy link
Contributor

This issue has been marked as stale because it has been open for 30 days with no activity. If the issue is still relevant then please leave a comment, or else it will be closed in 7 days.

@github-actions github-actions bot added the stale Issues about to be auto-closed label Aug 21, 2023
@github-actions
Copy link
Contributor

This issue has been closed because it has been stale for 7 days. You can re-open it if it is still relevant.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 28, 2023
@cjdoris cjdoris removed the stale Issues about to be auto-closed label Sep 22, 2023
@cjdoris cjdoris reopened this Sep 22, 2023
@cjdoris cjdoris closed this as completed Nov 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants