Re: [Python-Dev] can't set attributes of built-in/extension type
There is some discussion on this subject, archived here: http://permalink.gmane.org/gmane.comp.python.general/560661 I wonder if anyone could shed some light on this subject? (Or, help me understand, what is the difference between a type that I create using python C api and a python class?)
On Sat, Feb 23, 2008 at 4:55 PM, Neal Becker <ndbecker2@gmail.com> wrote:
There is some discussion on this subject, archived here: http://permalink.gmane.org/gmane.comp.python.general/560661
I wonder if anyone could shed some light on this subject?
(Or, help me understand, what is the difference between a type that I create using python C api and a python class?)
This is prohibited intentionally to prevent accidental fatal changes to built-in types (fatal to parts of the code that you never though of). Also, it is done to prevent the changes to affect different interpreters residing in the address space, since built-in types (unlike user-defined classes) are shared between all such interpreters. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum wrote:
On Sat, Feb 23, 2008 at 4:55 PM, Neal Becker <ndbecker2@gmail.com> wrote:
There is some discussion on this subject, archived here: http://permalink.gmane.org/gmane.comp.python.general/560661
I wonder if anyone could shed some light on this subject?
(Or, help me understand, what is the difference between a type that I create using python C api and a python class?)
This is prohibited intentionally to prevent accidental fatal changes to built-in types (fatal to parts of the code that you never though of). Also, it is done to prevent the changes to affect different interpreters residing in the address space, since built-in types (unlike user-defined classes) are shared between all such interpreters.
Thanks for the info. I'm still curious. What if I wanted to create a 'real' python class using python c-api? How is that done?
On Sun, Feb 24, 2008 at 6:49 AM, Neal Becker <ndbecker2@gmail.com> wrote:
Guido van Rossum wrote:
On Sat, Feb 23, 2008 at 4:55 PM, Neal Becker <ndbecker2@gmail.com> wrote:
There is some discussion on this subject, archived here: http://permalink.gmane.org/gmane.comp.python.general/560661
I wonder if anyone could shed some light on this subject?
(Or, help me understand, what is the difference between a type that I create using python C api and a python class?)
This is prohibited intentionally to prevent accidental fatal changes to built-in types (fatal to parts of the code that you never though of). Also, it is done to prevent the changes to affect different interpreters residing in the address space, since built-in types (unlike user-defined classes) are shared between all such interpreters.
Thanks for the info.
I'm still curious. What if I wanted to create a 'real' python class using python c-api? How is that done?
Use PyObject_Call() or one of its friends to call PyType_Type with the appropriate arguments (a name, a tuple of base classes, and a dict of methods etc.). This is the same as calling type(name, bases, namespace) from Python. The type object will be on the heap and fully mutable. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
(Or, help me understand, what is the difference between a type that I create using python C api and a python class?)
Grepping for the specific error message would have answered that question: Python (new-style) classes have the Py_TPFLAGS_HEAPTYPE set, types declared as static structs in C don't. Regards, Martin
Neal Becker wrote:
(Or, help me understand, what is the difference between a type that I create using python C api and a python class?)
Classes that you create in Python have a __dict__ attribute holding a dictionary for arbitrary attributes to go in. Most types defined in C don't bother providing a __dict__, since one doesn't normally need to add arbitrary attributes to them, and the overhead would be almost completely wasted. -- Greg
participants (4)
-
"Martin v. Löwis" -
Greg Ewing -
Guido van Rossum -
Neal Becker