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

G

George Sakkis

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
 
B

bruno modulix

George said:
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)
 
C

Cameron Laird

George Sakkis wrote: .
.
.
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)
.
.
.
That's one approach. Here's another: read Scott Meyer's
latest *Effective C++*, follow his advice, and become
familiar with idioms (and libraries, to the extent they're
available!) that make C++ a rather high-level language.
 
B

Bill McClain

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
 
D

D H

George said:
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.
 
K

Kay Schluehr

D said:
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/Patterns/C++Idioms/EuroPLoP98.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
 
G

George Sakkis

Kay Schluehr said:

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
 
J

Jorgen Grahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top