473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

C API eqivalent of object.__new__

How would one write object._new__(SomeType) in the C API ?

.... and what lies behind the following behaviour:
object.__new__(T)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object.__new__(T) is not safe, use T.__new__()

where T refers to almost any fundamental Python type ?
Jul 18 '05 #1
4 2052
Jacek Generowicz <ja**************@cern.ch> writes:
How would one write object._new__(SomeType) in the C API ?


PyInstance_NewRaw(PyObject *class, [PyObject* dict])

seems to be the way to do this for classic classes. How would one do
it for new-style classes?

Also, how would one create a new-style class "dynamically" in the C
API? IOW, what's the new-style equivalent of PyClass_New? or, put
another way, how would you write

type(name, (), dict)

in the C API ?
Jul 18 '05 #2
Jacek Generowicz <ja**************@cern.ch> wrote:
Jacek Generowicz <ja**************@cern.ch> writes:
How would one write object._new__(SomeType) in the C API ?
PyInstance_NewRaw(PyObject *class, [PyObject* dict])

seems to be the way to do this for classic classes. How would one do
it for new-style classes?


You mean something like:
PyObject_CallMethod( (PyObject*)&PyBaseObject_Type,
"__new__", "O", clas);
?
Also, how would one create a new-style class "dynamically" in the C
API? IOW, what's the new-style equivalent of PyClass_New? or, put
another way, how would you write

type(name, (), dict)

in the C API ?


You mean something like:
PyObject_CallFunctionObjArgs(
(PyObject*)&PyType_Type,
classname, classbases, classdict,
NULL);
?

There may be faster ways, but I generally start writing extensions by
keeping the C code as "close" to equivalent Python code as I can, which
means lots of use of PyObject_... level functions. If and when I need
to squeeze out the last few cycles, I may dig into those for
optimization purposes, but what was that quip about premature
optimization, again....?-)
Alex
Jul 18 '05 #3
al*****@yahoo.com (Alex Martelli) writes:
Jacek Generowicz <ja**************@cern.ch> wrote:
Jacek Generowicz <ja**************@cern.ch> writes:
How would one write object._new__(SomeType) in the C API ?


PyInstance_NewRaw(PyObject *class, [PyObject* dict])

seems to be the way to do this for classic classes. How would one do
it for new-style classes?


You mean something like:
PyObject_CallMethod( (PyObject*)&PyBaseObject_Type,
"__new__", "O", clas);
?
Also, how would one create a new-style class "dynamically" in the C
API? IOW, what's the new-style equivalent of PyClass_New? or, put
another way, how would you write

type(name, (), dict)

in the C API ?


You mean something like:
PyObject_CallFunctionObjArgs(
(PyObject*)&PyType_Type,
classname, classbases, classdict,
NULL);
?


I was indeed starting to think about PyObject_Call* approaches to
this, but didn't want to go down that path without making sure that I
haven't missed some function that was specifically designed to do it.

If _you_ don't know of one, then I'll take that to mean that I
shouldn't waste more time looking for it :-)

Thanks.
Jul 18 '05 #4
Jacek Generowicz <ja**************@cern.ch> wrote:
...
I was indeed starting to think about PyObject_Call* approaches to
this, but didn't want to go down that path without making sure that I
haven't missed some function that was specifically designed to do it.

If _you_ don't know of one, then I'll take that to mean that I
shouldn't waste more time looking for it :-)

Thanks.


You're welcome, but wrong (even though very kind!-) in assuming I
necessarily know every nook and cranny of Python's C API -- more often
than not, I focus on getting the job done, which means using the easiest
way (generally PyObject_somethingorother), then possibly go back looking
for potential acceleration, if and only if I need to optimize. "Make it
work, make it right, make it fast", as Kent Beck says!-). I've never
needed to optimize this specific case, so I wouldn't be surprised to
learn about better and faster approaches...
Alex
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michele Simionato | last post by:
Let me show first how does it work for tuples: >>> class MyTuple(tuple): .... def __new__(cls,strng): # implicit conversion string of ints => tuple .... return...
10
by: Stuart McGraw | last post by:
I have a class A from a third party that I cannot change and is implemented in C. I derive my own class B from A and add a couple new methods and override a method. The problem is that A has a...
5
by: could ildg | last post by:
As there is already __init__, why need a __new__? What can __new__ give us while __init__ can't? In what situations we should use __new__? And in what situations we must use __new__? Can __new__...
0
by: Timo | last post by:
I'm trying to make a thread safe object cache without locking. The objects are cached by the id of the data dict given in __new__. Objects are removed from the cache as soon as they are no longer...
3
by: Ron Adam | last post by:
I'm trying to implement simple svg style colored complex objects in tkinter and want to be able to inherit default values from other previously defined objects. I want to something roughly...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
7
by: inline | last post by:
Hello! I want to assign self to object of parent class in constructor, like def my_func(): ... return ParentClass() class MyClass (ParentClass): def __init__(self): self = my_func()
3
by: Steven W. Orr | last post by:
When I go to create an object I want to be able to decide whether the object is valid or not in __init__, and if not, I want the constructor to return something other than an object, (like maybe...
3
by: Torsten Mohr | last post by:
Hi, i have some questions related to new style classes, they look quite useful but i wonder if somebody can give me an example on constructors using __new__ and on using __init__ ? I just see...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.