-
-
Notifications
You must be signed in to change notification settings - Fork 61
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 __slots__ to structs #130
Comments
Note that Minimal repro: >>> from ctypes import *
>>>
>>> class TestStruct (Structure): pass
...
>>> TestStruct.__slots__ = ["a", "b"]
>>> s = TestStruct()
>>> s.c = 1
>>>
>>> class TestStruct (Structure):
... __slots__ = ["a", "b"]
...
>>> s = TestStruct()
>>> s.c = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'TestStruct' object has no attribute 'c' |
@corby Shouldn't this be kept open? I don't think ctypeslib provides slots yet? |
Apologies. I thought the point of the note was adding slots to the system would be pointless since it won't enforce frozen attributes. |
No: Slots are fine, you just have to get the declaration right. It's quite easy to shoot yourself in the foot with a typo in a struct field name, so it is a good idea to (correctly) set up slots on bindings generation to prevent assignment of non-existent fields. (Defining slots in the class body is not an issue with forward declarations, because they contain just the field names, no actual references.) Footnotes
|
This is an excellent tool and just what I was looking for. Thank you.
In order for the IDEs to recognize the fields as valid inputs we add
__slots__
to all of our ctype.Structs.Would it be possible to add a step to each struct def to add the slots?
e.g.:
The text was updated successfully, but these errors were encountered: