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

Add support for complex() using __complex__ #3185

Closed
wants to merge 5 commits into from

Conversation

bilhox
Copy link
Contributor

@bilhox bilhox commented Oct 20, 2024

This PR overloads the method __complex__ so you can convert a vector2 to a complex number using complex().
The usage ? Math only, definitely.
I'm supporting this PR as I know a lot of people using pygame for a non gamedev usage.

@bilhox bilhox added New API This pull request may need extra debate as it adds a new class or function to pygame math pygame.math labels Oct 20, 2024
@bilhox bilhox requested a review from a team as a code owner October 20, 2024 10:39
docs/reST/ref/math.rst Show resolved Hide resolved
src_c/math.c Outdated Show resolved Hide resolved
src_c/math.c Outdated
PyComplex_FromDoubles(self->coords[0], self->coords[1]);

if (complex == NULL) {
RAISE(PyExc_ValueError, "Couldn't build complex number from Vector2");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be return RAISE

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole function can just be return PyComplex_FromDoubles(self->coords[0], self->coords[1]); because the function already sets the python exception on error, we don't need to set an error ourselves

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the point of adding API for something that can be done in user code by typing just one more character than what this PR makes possible.

a = pygame.Vector2(1, 2)
complex(*a)

Bonus of complex(*a) is that it actually works on any 2-sized sequence like a tuple of list of numbers, which is also something some people use to represent coordinates instead of Vector2

src_c/math.c Outdated
PyComplex_FromDoubles(self->coords[0], self->coords[1]);

if (complex == NULL) {
RAISE(PyExc_ValueError, "Couldn't build complex number from Vector2");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole function can just be return PyComplex_FromDoubles(self->coords[0], self->coords[1]); because the function already sets the python exception on error, we don't need to set an error ourselves

@bilhox
Copy link
Contributor Author

bilhox commented Oct 20, 2024

I don't see the point of adding API for something that can be done in user code by typing just one more character than what this PR makes possible.

a = pygame.Vector2(1, 2)
complex(*a)

Bonus of complex(*a) is that it actually works on any 2-sized sequence like a tuple of list of numbers, which is also something some people use to represent coordinates instead of Vector2

It's one character less than what you proposed, which is still better. And also I think the tools Vector2 provides are way better than just list or tuple of 2 elements. In another side, It's not like it's something difficult for us to support, I mean, it's not like I have added a hundred of lines to bring support to it. (And I did not discuss yet why complex in the vector constructor is not supported)

@ankith26
Copy link
Member

Also, we have had debates about it in the past and back then we had consensus that it was not needed, atleast among the active reviewers at the time: #1515

@bilhox
Copy link
Contributor Author

bilhox commented Oct 20, 2024

With a lot of denial, I'm closing this PR.

@bilhox bilhox closed this Oct 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
math pygame.math New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants