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 __new__ ? #61

Closed
sebhahn opened this issue Mar 10, 2016 · 3 comments
Closed

why __new__ ? #61

sebhahn opened this issue Mar 10, 2016 · 3 comments

Comments

@sebhahn
Copy link

sebhahn commented Mar 10, 2016

How comes that you use the __new__ method with self, even though it is a static method by definition?

https://github.com/jswhit/pyproj/blob/master/lib/pyproj/__init__.py#L608

@micahcochran
Copy link
Collaborator

Geod and Proj both have __new__ methods. Looking at the python docs, it looks like self should be changed to cls for the __new__ method. This is something I noted while looking at this and does nothing to answer your question, sebhahn.

@micahcochran
Copy link
Collaborator

I tried __init__ vs __new__ with the Geod class this a little when I was fixing issues #64 (PR #65). IMHO Cython's documentation isn't clear enough about what is happening during object creation regarding __new__ and __cinit__. So, some of this is my conjecture about how the functions __init__, __cinit__, and __new__.

For Proj, the __new__() is called first, which subsequently calls _proj.Proj.__new__(), which I assume is an alias for the __cinit__(). If you were to rename the Proj class function to __init__() it would happen after the function has been initialized. What is in __new__() is python code that parses parameters or makes ellipse calculations and passes it up to something that the __cinit__ which interfaces with the rest of the C code (proj.4 library).

That could probably be moved directly into the Cython __cinit__ functions, but then the code would also have to be compiled along with the rest of it.

The __new__ function has a cls parameter (which is currently confusingly called self as mentioned previously), which should not be stored to because the memory has not yet be allocated, which was what was happening in issue #64 .

TLDR The best answer is that __new__ was a design decision to add functionality in python code, then calls the corresponding Cython code to initialize the C library facing parts. __init__ would happen after __cinit__ meaning that the code in __new__ would not be able to feed into __cinit__.

@snowman2
Copy link
Member

This has changed in the most recent version 2.1.0.

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

3 participants