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

Can python control complicated classes/objects written in C++

Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)
data = new TData( option1=..., option2=...)
action1 = new TAction(option1=range(1,10)...)
action2 = new TSubAction(option1=sin(5),..., option2=...)
data.run( action1, action2)
data.print


The benefits here is that I do not have to worry about user-input
(python handles functions like range(), and numeric/string operations
for me and send them to my objects), running logic (user create the
scripts) and need only focus on the objects themselves. It would be
better if users can easily extend TAction by themselves, through either
Python or C++.

I am totally new to Python. I have read boost.python, python extension
document but still do not know exactly what to do. My questions are:

1. can Python fully read/write member data and run member functions of
my objects?

2. can python pass complicated objects (TAction) to another object (TData)?

3. If python can not do this, I will have to create my own scripting
language. Given the above pseudo code, any suggestion on how to
implement it? I have googgled Qt Script for Application and many other
weird implementations but I either do not like the grammar of the script
language or the huge overheads.

Many thanks in advance.

Bo
Jul 18 '05 #1
7 2366
If you can think it, Python can do it. To get a feel for how it works,
take a look at http://python.org/doc/2.3.4/ext/ext.html. Using the C
API, you can make Python do anything.

However, writing all the glue can be boring and error prone, so there
are a number of tools to make this easier. Take a look at
http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ and
http://swig.sourceforge.net/ for ideas.

On Fri, Jun 04, 2004 at 11:24:38PM -0500, Bo Peng wrote:
Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

[snip]

I am totally new to Python. I have read boost.python, python extension
document but still do not know exactly what to do. My questions are:

1. can Python fully read/write member data and run member functions of
my objects?

2. can python pass complicated objects (TAction) to another object (TData)?

3. If python can not do this, I will have to create my own scripting
language. Given the above pseudo code, any suggestion on how to
implement it? I have googgled Qt Script for Application and many other
weird implementations but I either do not like the grammar of the script
language or the huge overheads.

Many thanks in advance.

Bo


Jul 18 '05 #2

"Bo Peng" <bp***@rice.edu> wrote in message
news:c9**********@joe.rice.edu...
Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)
> data = new TData( option1=..., option2=...)
> action1 = new TAction(option1=range(1,10)...)
> action2 = new TSubAction(option1=sin(5),..., option2=...)
> data.run( action1, action2)
> data.print

If you remove 'new' on the first three lines, the above could be Python.
The benefits here is that I do not have to worry about user-input
(python handles functions like range(), and numeric/string operations
for me and send them to my objects), running logic (user create the
scripts) and need only focus on the objects themselves. It would be
better if users can easily extend TAction by themselves, through either
Python or C++.

I am totally new to Python. I have read boost.python, python extension
document but still do not know exactly what to do. My questions are:

1. can Python fully read/write member data and run member functions of
my objects?
If they are properly wrapped, yes.
2. can python pass complicated objects (TAction) to another object

(TData)?

Everything in Python is a first class object that can be passed, returned,
or whatever.

Phil's response:
<However, writing all the glue can be boring and error prone, so there
<are a number of tools to make this easier. Take a look at
<http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ and

PyRex intends to make it easy to write C extensions in extended Python.

<http://swig.sourceforge.net/ for ideas.

swig wraps existing extensions, which you seem to have.

Terry J. Reedy


Jul 18 '05 #3
"Bo Peng" <bp***@rice.edu> wrote in message
news:c9**********@joe.rice.edu...
Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)
> data = new TData( option1=..., option2=...)
> action1 = new TAction(option1=range(1,10)...)
> action2 = new TSubAction(option1=sin(5),..., option2=...)
> data.run( action1, action2)
> data.print


<snip>


To follow up on Terry Reedy's comment, why are the objects in C++ at all?
Or at least, why not prototype this entirely in Python, and then use what
you've learned to architect a good solid set of C++ classes? If the classes
are as complicated as you say, then there is a good chance that your initial
class and class interaction designs will need some refining after you get
them down in rough form. As your design thoughts evolve about the
interfaces of the TData and TAction classes, and how they work together, you
will be much quicker at making the changes in Python.

-- Paul

Jul 18 '05 #4
Bo Peng wrote:
I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.


The example you give will be no problem using SWIG and Python.

In general the area where things get tricky is dealing with
memory management. You have to decide who "owns" any returned
objects - do they belong to the C++ code and have to be freed
via method calls, or do they belong to the Python proxy object
which frees them when it is deleted.

The former case can lead to memory leaks and the latter case
can lead to things being freed that are referenced by other
C++ objects.

You can have difficulty in this area if the underlying C++ library
is poorly designed. For example I have had to deal with cases
where the C++ objects had references between each other and it
was really hard to figure out when to free the objects. Mind you I
would encounter the same issues if using the library from other
C++ code. In the end I had to write a C++ wrapper on top of the
C++ library and deal with reference counting and that kind of
stuff in the wrapper. The wrapper trivially exported to Python
via SWIG.

I guess the point I am making in a long winded way is that if
the library is poorly designed, especially with respect to
objects ownership issues, then Python won't make them go away
and may highlight them more since more code paths become
possible.

Roger
Jul 18 '05 #5
Bo Peng wrote:
Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)
> data = new TData( option1=..., option2=...)
> action1 = new TAction(option1=range(1,10)...)
> action2 = new TSubAction(option1=sin(5),..., option2=...)
> data.run( action1, action2)
> data.print


The benefits here is that I do not have to worry about user-input
(python handles functions like range(), and numeric/string operations
for me and send them to my objects), running logic (user create the
scripts) and need only focus on the objects themselves. It would be
better if users can easily extend TAction by themselves, through either
Python or C++.

I am totally new to Python. I have read boost.python, python extension
document but still do not know exactly what to do. My questions are:

1. can Python fully read/write member data and run member functions of
my objects?

2. can python pass complicated objects (TAction) to another object
(TData)?

3. If python can not do this, I will have to create my own scripting
language. Given the above pseudo code, any suggestion on how to
implement it? I have googgled Qt Script for Application and many other
weird implementations but I either do not like the grammar of the script
language or the huge overheads.

Many thanks in advance.

Bo


You can try the Python module in Boost ( http://boost.org/ )
Here's a direct link : http://boost.org/libs/python/doc/index.html

Jul 18 '05 #6
djw
Bo Peng wrote:
Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)
> data = new TData( option1=..., option2=...)
> action1 = new TAction(option1=range(1,10)...)
> action2 = new TSubAction(option1=sin(5),..., option2=...)
> data.run( action1, action2)
> data.print


The benefits here is that I do not have to worry about user-input
(python handles functions like range(), and numeric/string operations
for me and send them to my objects), running logic (user create the
scripts) and need only focus on the objects themselves. It would be
better if users can easily extend TAction by themselves, through either
Python or C++.

I am totally new to Python. I have read boost.python, python extension
document but still do not know exactly what to do. My questions are:

1. can Python fully read/write member data and run member functions of
my objects?

2. can python pass complicated objects (TAction) to another object (TData)?

3. If python can not do this, I will have to create my own scripting
language. Given the above pseudo code, any suggestion on how to
implement it? I have googgled Qt Script for Application and many other
weird implementations but I either do not like the grammar of the script
language or the huge overheads.

Many thanks in advance.

Bo


I would take a look at PyQT as an example of a "complicated" C++ library
(Qt) wrapped for Python. Iys uses SIP as the interface generator, but
other projects would also work for you (SWIG, Boost, etc)

-Don

Jul 18 '05 #7
I am planning on an application that involves several complicated C++
classes. ... I would like to use a script
language to control the interaction between these c++ objects.


Thank you all for the good (and quick) suggestions. I have read more
about Python extension (SWIG, especially Dr. Beazley's tutorial) and
boost.Python. It seems that Python/C++ is the way to do.

1. I have to use C++ since efficiency is at very high priority.

2. Writing a prototype in Python is a very good idea. Since I am more
proficient in C++ than in Python, I am not sure which way is quicker for
me. I will read more about Python and decide.

3. SWIG can provide tcl, perl interfaces as well. However, perl's OOP
features seem to be weird and tcl's syntax is kind of old (personal
opinions). Python's integration with C++ is better and the syntax is
more natural.

4. Boost.python and SWIG are similar but SWIG is easier to use. I will
try SWIG first.

5. My C++ code will use a lot of functors (passing objects that have
operator() ), templates, inheritance. I hope SWIG/Python can handle all
of them... I am not sure if Python/SWIG can handle copy constructor
transparently either. I am reading the documents.

Again, thank you all for the help.
Bo
Jul 18 '05 #8

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.