Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21599 > unrolled thread
| Started by | Dids <didierblanchard@gmail.com> |
|---|---|
| First post | 2012-03-14 06:46 -0700 |
| Last post | 2012-03-14 07:28 -0700 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
Instantiate a python class object in C Dids <didierblanchard@gmail.com> - 2012-03-14 06:46 -0700
Re: Instantiate a python class object in C Stefan Behnel <stefan_ml@behnel.de> - 2012-03-14 15:13 +0100
Re: Instantiate a python class object in C Dids <didierblanchard@gmail.com> - 2012-03-14 07:28 -0700
| From | Dids <didierblanchard@gmail.com> |
|---|---|
| Date | 2012-03-14 06:46 -0700 |
| Subject | Instantiate a python class object in C |
| Message-ID | <500e1575-78e7-45aa-8fc3-78427bbe917c@l7g2000vbw.googlegroups.com> |
Hi,
Apologies if this was asked before, I couldn't find anything.
I have a class defined in a python file:
for example:
class demo:
[ class definition goes here]
I'm writing a C extension.
In the first function, I take an instance of the "demo" class and do
my magic. It's working, all is good.
What I can't figure out is how to create a second C function that
returns a new instance to the "demo" class to python.
There must a be tutorial somewhere, but I can't find anything. I do
not want to define a new python class in C.
Another example:
This is working:
demo_obj1 = demo()
my_C_extension.function_1( demo_obj1 ) //working, all good.
This I can't figure out how to do:
new_demo_obj = my_C_extension.function_2()
Thanks for reading,
Dids,
[toc] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2012-03-14 15:13 +0100 |
| Message-ID | <mailman.635.1331734407.3037.python-list@python.org> |
| In reply to | #21599 |
Dids, 14.03.2012 14:46: > Apologies if this was asked before, I couldn't find anything. > > I have a class defined in a python file: > for example: > > class demo: > [ class definition goes here] > > I'm writing a C extension. > In the first function, I take an instance of the "demo" class and do > my magic. It's working, all is good. > > What I can't figure out is how to create a second C function that > returns a new instance to the "demo" class to python. > There must a be tutorial somewhere, but I can't find anything. I do > not want to define a new python class in C. > > Another example: > This is working: > demo_obj1 = demo() > my_C_extension.function_1( demo_obj1 ) //working, all good. > > This I can't figure out how to do: > new_demo_obj = my_C_extension.function_2() You should consider giving Cython a try. It will allow you to write normal Python code for your C extension that it translates to efficient C code. This is much easier than writing all of this by hand, especially when it comes to classes. It will also optimise the code for you, so that you'll often end up with faster code than what you'd manually write. Stefan
[toc] | [prev] | [next] | [standalone]
| From | Dids <didierblanchard@gmail.com> |
|---|---|
| Date | 2012-03-14 07:28 -0700 |
| Message-ID | <44ca54da-a2ca-48ff-83e7-9de18b064f0e@y10g2000vbn.googlegroups.com> |
| In reply to | #21601 |
Ok, I have it :) PyImport_Import , PyModule_GetDict, PyDict_GetItemString and PyObject_CallObject Need to take a second look at cython when I have a spare cycle or 2. Thanks for the the tip :) A+ Dids,
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web