#extensions Selecting which class to construct depending on input

The situation is as follows.

There exists map class in builtins. I wrote another class map1 in C.

I would like is to incorporate map into map1 so that if map1_init was called with more than 2 arguments, it would construct map and map1 otherwise. I thought of just creating another function and doing this logic outside class constructors - it would seem a clean approach. However I can not figure out how to construct extension classes from C.

Performance is crucial here as map1 offers fairly small performance boost. E.g. wrapping those 2 classes in pure python function is too slow.

Any help would be appreciated.

Just like from python, by calling the corresponding type object.

1 Like

Thanks, makes sense. Although it doesn’t get me where I want to get. This results in 80ns overhead. I need it to be max 20ns for what I do to be worthwhile. e.g. functools.partial only results in 10ns overhead for partially set function of 2 arguments. partial(opr.eq, 1)

from operator import eq
from functools import partial

%timeit eq(1, 1)    # 35.1 ns
eqp = partial(eq, 1)
%timeit eqp(1)     # 48 ns

BTW, wrong thread. Was meant to post to “Python Help”. Maybe someone can move it there?

Apparently I have this power despite not being a moderator, so there you go.

1 Like