By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Back to the future - python to C++ advice wanted

P: n/a
During the last 18 months or so I have indulged in the joy of learning
and using python for almost everything, but I may have to go back to
C/C++ at work. Suddenly I found myself transliterating (or translating
at least) common python idioms and patterns, looking for libraries to
replace python's "included batteries" or writing my own from scratch,
(over)using templates in an attempt to mimic duck typing, and so on.
Still, I am not sure if this is a good idea in general since every
language has its own idiosyncrasies, and this is obvious when one sees
python code looking like C or Java. OTOH, bringing python's higher
level of expressiveness to C/C++ might actually be a good thing,
leading to cleaner, safer code.

So, I wonder what have others who have gone the same path done and
learned in similar situations. How one can avoid the frustration of
having to work with a low level language once he has seen the Light ?

George

Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
George Sakkis wrote:
During the last 18 months or so I have indulged in the joy of learning
and using python for almost everything, but I may have to go back to
C/C++ at work. (snip) So, I wonder what have others who have gone the same path done and
learned in similar situations. How one can avoid the frustration of
having to work with a low level language once he has seen the Light ?

Well, I'm lucky enough to not to have to use C++ again, so I can't
answer directly. But I think this might interest you:
http://www.boost.org/
(NB : never tried it myself, but seems to be a nice job)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '05 #2

P: n/a
In article <42**********************@news.free.fr>,
bruno modulix <on***@xiludom.gro> wrote:
George Sakkis wrote:

Jul 19 '05 #3

P: n/a
On 2005-06-17, George Sakkis <gs*****@rutgers.edu> wrote:
So, I wonder what have others who have gone the same path done and
learned in similar situations. How one can avoid the frustration of
having to work with a low level language once he has seen the Light ?


This project:

http://astrolabe.sourceforge.net/

....is implemented in both Python and C++. I do the Python version first and
then translate to C++ as directly as possible, using namespaces, exception
handling, the standard string type and STL library.

Let me second the recommendation of the Scott Meyers books. And another which
I no longer have -- something like "The C++ Memory Model" -- was very
instructive.

-Bill
--
Sattre Press In the Quarter
http://sattre-press.com/ by Robert W. Chambers
in**@sattre-press.com http://sattre-press.com/itq.html
Jul 19 '05 #4

P: n/a
D H
George Sakkis wrote:
During the last 18 months or so I have indulged in the joy of learning
and using python for almost everything, but I may have to go back to
C/C++ at work. Suddenly I found myself transliterating (or translating
at least) common python idioms and patterns, looking for libraries to
replace python's "included batteries" or writing my own from scratch,
(over)using templates in an attempt to mimic duck typing, and so on.
Still, I am not sure if this is a good idea in general since every
language has its own idiosyncrasies, and this is obvious when one sees
python code looking like C or Java. OTOH, bringing python's higher
level of expressiveness to C/C++ might actually be a good thing,
leading to cleaner, safer code.

So, I wonder what have others who have gone the same path done and
learned in similar situations. How one can avoid the frustration of
having to work with a low level language once he has seen the Light ?


That's why so many people have switched to Java or C# (or Python and
other langugages of course). You might talk to them about using Python,
since Python and C/C++ fit together very nicely (with tools like BOOST,
SWIG, Pyrex,...). But me personally I like to avoid C++ altogether when
possible, and use Java or C# instead, both of which also can be combined
with python or python-like languages such as jython, groovy, or boo.
Jul 19 '05 #5

P: n/a
D H wrote:
That's why so many people have switched to Java or C# (or Python and
other langugages of course). You might talk to them about using Python,
since Python and C/C++ fit together very nicely (with tools like BOOST,
SWIG, Pyrex,...). But me personally I like to avoid C++ altogether when
possible, and use Java or C# instead, both of which also can be combined
with python or python-like languages such as jython, groovy, or boo.


But accessing Java packages from jython does not reduce effecively the
underlying pain. Personally I favour for programming in C++ over Java,
because I feel at least the mind of an evil genius ( C++ template
programming is Turing complete, operator overloading enables objects
having a builtin groove ) where Java is plain mediocrity. In spirit it
is a kind of Anti-Python.

I recommend studying C++ idioms carefully.

http://www1.bell-labs.com/user/cope/...uroPLoP98.html

If Georges starts on greenfields he may have a look at Qt and it's
object library which is not only concerned with widgets.

http://doc.trolltech.com/3.3/

BOOST is more high brow and I guess that it compiles slow because it
uses templates extensively. Template metaprogramming as a compile time
language was a funny discovery. Here is some prove of it's
capabilities:

http://osl.iu.edu/~tveldhui/papers/2003/turing.pdf

Regards,
Kay

Jul 19 '05 #6

P: n/a
"Kay Schluehr" wrote:
I recommend studying C++ idioms carefully.

http://www1.bell-labs.com/user/cope/...uroPLoP98.html
Thanks for the link; very useful indeed.
If Georges starts on greenfields he may have a look at Qt and it's
object library which is not only concerned with widgets.

http://doc.trolltech.com/3.3/

BOOST is more high brow and I guess that it compiles slow because it
uses templates extensively. Template metaprogramming as a compile time
language was a funny discovery. Here is some prove of it's
capabilities:

http://osl.iu.edu/~tveldhui/papers/2003/turing.pdf


Many thanks to Kay and Bruno for suggesting Boost; I browsed through
its numerous libraries and they're quite impressive ! They seem
indispensable, especially for python (or other very high level
language) programmers going back to C++. Some libraries that seem to be
very relevant to pythoneers are:

- any: brings dynamic typing in C++
- tuple; 'nuff said
- iterator: out-of-the-box equivalents of
itertools.{imap,ifilter,izip,count}, reversed(), and others not
existing or applicable in python
- tokenizer, string_algo and regex: similar functionality to str.* and
re.*
- bind, mem_fn, function, functional, lambda: first class callables,
currying, higher order (functional) programming
- assign: syntactic sugar through operator overloading for (relatively)
readable container initialization:
map<int,int> next = map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);
is actually valid and equivalent to
next = dict([(1,2), (2,3), (3,4), (4,5), (5,6)])
- many, many more goodies, with or without respective standard python
equivalent (threads, graphs, math utils, serialization,
metaprogramming, etc).
- and last but not least, Boost.Python. I don't think it's just a
coincidence that among all languages they chose Python to make
interoperable with C++ :-)

Thanks again,
George

Jul 19 '05 #7

P: n/a
On 17 Jun 2005 06:26:50 -0700, George Sakkis <gs*****@rutgers.edu> wrote:
During the last 18 months or so I have indulged in the joy of learning
and using python for almost everything, but I may have to go back to
C/C++ at work. Suddenly I found myself transliterating (or translating
at least) common python idioms and patterns, looking for libraries to
replace python's "included batteries" or writing my own from scratch,
(over)using templates in an attempt to mimic duck typing, and so on.
Still, I am not sure if this is a good idea in general since every
language has its own idiosyncrasies, and this is obvious when one sees
python code looking like C or Java.

So, I wonder what have others who have gone the same path done and
learned in similar situations.
I feel exactly like you described, both when I move from C++ to Python, and
when I move from Python to C++. I think most multi-language programmers
feel like this, and I don't think there's anything wrong with it.

What I do? I try to keep calm and resist the temptation to write C++ code
in Python and the other way around. Why? Because I know how much I hate C++
code written as if it was Java, or C, or Smalltalk.

(rearranged from above) OTOH, bringing python's higher
level of expressiveness to C/C++ might actually be a good thing,
leading to cleaner, safer code.
Maybe. On the other hand, you risk messing up the things that make idiomatic
C++ clean and safe: static typing, const correctness, conservative use of
inheritance and polymorphism, ... And in the end: will other C++ programmers
hate your code? Will they modify it without butchering your original
design?
How one can avoid the frustration of
having to work with a low level language once he has seen the Light ?


I wouldn't call the modern C++ you (since you don't seem to be afraid of
templated) use a "low level language". It does different choices compared to
Python when it comes to run-time efficiency versus usability, and so on.

But yes, programming in C++ is usually more tedious and frustrating -- and
less fun.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.