473,287 Members | 1,793 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,287 software developers and data experts.

C++ version of the C Python API?

Hi,

Is there a C++ version of the C Python API packaged with python 2.5?
It would be nice to have a OOP approach to embedding python in C++. It
would also be a bonus if this C++ Python API cleaned up a lot of the
messy code involved in embedding python.

Thanks.
Oct 19 '07 #1
14 1641
On Fri, 19 Oct 2007 17:37:29 -0500, Robert Dailey wrote:
Hi,

Is there a C++ version of the C Python API packaged with python 2.5? It
would be nice to have a OOP approach to embedding python in C++. It
would also be a bonus if this C++ Python API cleaned up a lot of the
messy code involved in embedding python.

Thanks.
Perhaps, the remark about `C++ extensions`_ or `Embedding in C++`_ in the
`Extending and Embedding`_ docs (described on doc.python.org as a
"tutorial for C/C++ programmers") can help.

Depending on your level of embedding, those random annotations spread
over the extending docs, found with a little patience and ctrl+f, might
help:

* "In C++, the operators new and delete are used with essentially the
same meaning and we'll restrict the following discussion to the C case."
-- `1.10 Reference counts <http://docs.python.org/ext/refcounts.html>`_
* "Note that PyMODINIT_FUNC declares the function as void return type,
declares any special linkage declarations required by the platform, and
for C++ declares the function as extern "C"." -- `1.4 The Module's Method
Table and Initialization Function <http://docs.python.org/ext/
methodTable.html>`_

Cheers,
Stargaming

... _C++ extensions: http://docs.python.org/ext/cplusplus.html
... _Embedding in C++: http://docs.python.org/ext/embeddingInCplusplus.html
... _Extending and embedding: http://docs.python.org/ext/ext.html
Oct 20 '07 #2
Is there a C++ version of the C Python API packaged with python 2.5?

Stargaming has already mentioned the fine points; the first answer is:
yes, the API packaged python 2.5 can be used with C++. It is a C++
version of the same API as it adds proper extern "C" declarations around
all prototypes, and it was specifically cleaned up (ten years ago)
to work with C++.

This API does not make use of many of the C++ features, including
classes, templates, or overloading.

Regards,
Martin
Oct 21 '07 #3
Is there a C++ version of the C Python API packaged with python 2.5?

Stargaming has already mentioned the fine points; the first answer is:
yes, the API packaged python 2.5 can be used with C++. It is a C++
version of the same API as it adds proper extern "C" declarations around
all prototypes, and it was specifically cleaned up (ten years ago)
to work with C++.

This API does not make use of many of the C++ features, including
classes, templates, or overloading.

Regards,
Martin
Oct 21 '07 #4
On 10/21/07, "Martin v. Löwis" <ma****@v.loewis.dewrote:
Is there a C++ version of the C Python API packaged with python 2.5?

Stargaming has already mentioned the fine points; the first answer is:
yes, the API packaged python 2.5 can be used with C++. It is a C++
version of the same API as it adds proper extern "C" declarations around
all prototypes, and it was specifically cleaned up (ten years ago)
to work with C++.

This API does not make use of many of the C++ features, including
classes, templates, or overloading.

Regards,
Martin
Well C++ implicitly includes OOP since that is the foundation of the
language. I was more or less asking if there was an object oriented
version of the Python embedded API or perhaps an OO wrapper. However
it doesn't seem that way, so I may have to make my own.

Thanks for the responses.
Oct 21 '07 #5
Well C++ implicitly includes OOP since that is the foundation of the
language. I was more or less asking if there was an object oriented
version of the Python embedded API or perhaps an OO wrapper. However
it doesn't seem that way, so I may have to make my own.
I think you are misinterpreting what you are seeing. The Python C API
*is* object-oriented. It has all features of object-orientation:
classes, encapsulation, polymorphism, late binding, ...

As for "make your own": people have tried this before; there are
multiple C++ wrappers around the Python C API available.

Regards,
Martin
Oct 21 '07 #6
On 10/21/07, "Martin v. Löwis" <ma****@v.loewis.dewrote:
I think you are misinterpreting what you are seeing. The Python C API
*is* object-oriented. It has all features of object-orientation:
classes, encapsulation, polymorphism, late binding, ...

As for "make your own": people have tried this before; there are
multiple C++ wrappers around the Python C API available.

Regards,
Martin
Could you emphasize a little more? I haven't worked much at all with
the Python C API, so I may be misunderstanding. First of all, you say
that the "Python C API is object oriented", which is contradictory
because it should read "Python C++ API is object oriented". Perhaps
this is a typo, or maybe you're referencing some C++ wrapper for the
Python C API that you failed to mention the name of.

You also mentioned that there are "multiple C++ wrappers arround the
Python C API Available"... could you provide names for a few of the
popular ones?

Thanks again!
Oct 21 '07 #7
Could you emphasize a little more? I haven't worked much at all with
the Python C API, so I may be misunderstanding. First of all, you say
that the "Python C API is object oriented", which is contradictory
because it should read "Python C++ API is object oriented". Perhaps
this is a typo, or maybe you're referencing some C++ wrapper for the
Python C API that you failed to mention the name of.
No, I literally meant that the Python C API is object-oriented.
You don't need an object-oriented language to write object-oriented
code.
You also mentioned that there are "multiple C++ wrappers arround the
Python C API Available"... could you provide names for a few of the
popular ones?
The most popular ones are Boost.Python, CXX, and PySTL.

Regards,
Martin
Oct 21 '07 #8
On 10/21/07, "Martin v. Löwis" <ma****@v.loewis.dewrote:
No, I literally meant that the Python C API is object-oriented.
You don't need an object-oriented language to write object-oriented
code.
I disagree with this statement. C is not an object oriented language,
and I've seen attempts to make it somewhat object oriented, however it
failed miserably in readability and manageability overhead. However,
this isn't the place to discuss such a thing so I've got nothing more
to say than that.

I do appreciate you taking the time to respond to my inquiry and offer
a few C++ wrapper API's for the Python C API. Take care!
Oct 21 '07 #9
"Martin v. Löwis" <ma****@v.loewis.dewrote:
...
The most popular ones are Boost.Python, CXX, and PySTL.
I think SIP is also pretty popular (see
<http://www.riverbankcomputing.co.uk/sip/>).
Alex
Oct 21 '07 #10
On Sun, 21 Oct 2007 16:17:19 -0500, Robert Dailey wrote:
On 10/21/07, "Martin v. Löwis" <ma****@v.loewis.dewrote:
>No, I literally meant that the Python C API is object-oriented. You
don't need an object-oriented language to write object-oriented code.

I disagree with this statement. C is not an object oriented language,
and I've seen attempts to make it somewhat object oriented, however it
failed miserably in readability and manageability overhead. However,
this isn't the place to discuss such a thing so I've got nothing more to
say than that.
What he means is that the C API provides a complete, if boilerplate-
heavy, interface to object oriented aspects of Python. I.e., you can
write Python types completely in C, including all the OOPy stuff like
inheritance and so on. You don't need a language with built-in support
of OOP to do that.

Now, a C++ API for CPython would necessarily be built on top of the C
API, which carries some limitations relative to the OOP abilities of C++
itself. I suspect all you'll get from a C++ binding is a slightly more
comfortable (to people who like C++) calling interface. It could help
bring some unity to your extension code, and maybe get rid of a few
typecasts and simplify function names. But you shouldn't expect anything
like the ability to freely inherit between C++ and Python classes.
Carl Banks
Oct 21 '07 #11
Robert Dailey wrote:
On 10/21/07, "Martin v. Löwis" <ma****@v.loewis.dewrote:
>No, I literally meant that the Python C API is object-oriented.
You don't need an object-oriented language to write object-oriented
code.

I disagree with this statement. C is not an object oriented language,
and I've seen attempts to make it somewhat object oriented, however it
failed miserably in readability and manageability overhead. However,
this isn't the place to discuss such a thing so I've got nothing more
to say than that.
Guess you haven't programmed in GTK+ on C, then. Very heavy in
object-oriented programmin. C++'s OOP stuff is just pure syntactic
sugar, nothing more, nothing less.

Python's OO nature is a bit deeper, since we're talking a dynamic
language. Scheme, another dynamic language, is not inherently OO or
non-OO. You can use it in either fashion.
>
I do appreciate you taking the time to respond to my inquiry and offer
a few C++ wrapper API's for the Python C API. Take care!
Any C++ version of the python API is by definition going to be a wrapper
around the C version. Even the C version is a wrapper around the python
object model.
Oct 21 '07 #12
In article <ma**************************************@python.o rg>,
Robert Dailey <rc******@gmail.comwrote:
>
Is there a C++ version of the C Python API packaged with python 2.5?
It would be nice to have a OOP approach to embedding python in C++. It
would also be a bonus if this C++ Python API cleaned up a lot of the
messy code involved in embedding python.
One other thing: you may get more advice from C++-sig and/or capi-sig;
mail.python.org has the info for subscribing to those.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

The best way to get information on Usenet is not to ask a question, but
to post the wrong information.
Oct 22 '07 #13
On 10/21/07, Carl Banks <pa************@gmail.comwrote:
Now, a C++ API for CPython would necessarily be built on top of the C
API, which carries some limitations relative to the OOP abilities of C++
itself.
It wouldn't have to be, although it'd be much more of a maintenance
nightmare if it poked into the Python internals.
But you shouldn't expect anything like the ability to freely inherit between
C++ and Python classes.
You can do this with Boost.Python.

--
Nick
Oct 22 '07 #14
On 10/21/07, Robert Dailey <rc******@gmail.comwrote:
On 10/21/07, "Martin v. Löwis" <ma****@v.loewis.dewrote:
No, I literally meant that the Python C API is object-oriented.
You don't need an object-oriented language to write object-oriented
code.

I disagree with this statement. C is not an object oriented language,
and I've seen attempts to make it somewhat object oriented, however it
failed miserably in readability and manageability overhead. However,
this isn't the place to discuss such a thing so I've got nothing more
to say than that.
Object-oriented programming is a design choice, not a language
feature. You can write straight procedural code in C++, and you can
write object oriented code in C. Sure, C++ has some language features
which facilitate object-oriented programming, but it doesn't magically
make your code object-oriented. You can certainly write basic
object-oriented code in C and hide most of the implementation in
preprocessor macros if you so desire.

--
Nick
Oct 22 '07 #15

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

Similar topics

16
by: Manlio Perillo | last post by:
Hi. I'm a new user of Python but I have noted a little problem. Python is a very good language but it is evolving, in particular its library is evolving. This can be a problem when, ad example,...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
2
by: David Smith | last post by:
I currently have Python 2.2 and 2.3.4 installed. I want to install Python 2.4, and erase 2.3.4, but retain 2.2, for I need it for my connectivity program. According to the the documentation: ...
1
by: Marcio Rosa da Silva | last post by:
Hi all, My first try on using RST to write an email. so I can (will) make mistakes :-) I am experiencing the problem shown in this thread_ of the `python-mode`_ discussion list hosted at...
6
by: manatlan | last post by:
I've got a trouble, and i think that anybody there can help me I've got a python script which i distribute in somes packages for *nix. This script is full of python and need python 2.4 ! And i'd...
5
by: David C. Ullrich | last post by:
Would there be issues (registry settings, environment variables, whatever) if a person tried to install versions 1.x and 2.x simultaneously on one Windows system? Windows 98, if it matters. (I...
6
by: king kikapu | last post by:
Hi to all, i started with Python at v2.5 and now i see that a new version is released. As i already have a lot of stuff for Python installed in the site- packages directory, which is the...
3
by: Jeffrey Froman | last post by:
Hello All, I have two python versions installed, one in /usr/bin, and one in /usr/local/bin. However, when invoking python without a full path, I get the wrong executable with the right...
6
by: dogatemycomputer | last post by:
Greetings, A friend of mine dropped off a copy of Sams Teach Yourself Python in 24 Hours published in 2000. I skimmed the first couple of chapters looking for the interpreter version and the...
4
by: njwilson23 | last post by:
I'm having trouble with tkinter on a new installation of Python (2.6), built with the framework option from source that was downloaded from python.org. I'm running OS 10.4 on a PowerPC G4. The...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.