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

Support creating class member variables (not instance members) #662

Closed
programmerjake opened this issue Nov 12, 2019 · 3 comments
Closed

Comments

@programmerjake
Copy link
Contributor

This can apparently be done by setting PyTypeObject.tp_dict to a dict with the appropriate values before calling PyType_Ready.

See https://stackoverflow.com/questions/35954016/defining-an-inner-class-using-the-python-c-api for a use case. Another use case would be creating enum constants (assuming initialization order can be worked out).

I think a useful way to do that would be to have an overridable static_init function that returns the PyDict used to initialize tp_dict, since that also allows non-const code to create the member variables.

@davidhewitt
Copy link
Member

@programmerjake with #905 and #914 is there any more work to be done here? I think we established that the class attributes (as we decided to call them) can't be mutable, so I believe we've covered everything...

@programmerjake
Copy link
Contributor Author

I think that should be all that's needed, unless we want to add a function (static_init from first comment) that can add members algorithmically, though that would have to be unsafe since it could save a copy of the dict somewhere and try to modify it after initialization finished.

@davidhewitt
Copy link
Member

Let's call this done, as nobody else has asked for more on this front since we added this over a year ago. We can open a new issue if there are real-world use cases for the static_init case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants