Converting Python app to C++ completely

Sean 'Shaleh' Perry shalehperry at attbi.com
Thu Aug 29 14:00:29 EDT 2002


On Thursday 29 August 2002 10:37, Alan James Salmoni wrote:
>
> 1) How easy is it to convert existing Python code to C++? I read that
> each line of C++ code is roughly equivilent to 5-8 lines of C++ code,
> but is the conversion a simple task or is it troublesome?
>

class Point:
    def __init__(self):
        self.x = 0
        self.y = 0 

class Point {
private:
    int x, y;
public:
    Point(void): x(0), y(0) { /* do nothing here */ }
};

Most of your extra lines come from having to statically type everything.

> 2) Are there any pitfalls to beware of - I understand that dealing with
> memory and pointers might be a problem, but like I said earlier, I just
> don't really know?
>
> 3) Does C++ have equivilent functional constructs like reduce and map?
> These helped to make my code much cleaner, tighter and faster, and I
> would miss them dearly.
>

Josuttis' "The C++ Standard Library" is a must read.  The STL gives you a lot 
of what Python has built in.

for_each(container.begin(), container.end(), function);

is the equivalent of map() except it does not return a new list.  Implementing 
filter takes a bit more work but can be done.

In general C++ will be more verbose than Python.  Using references and the STL 
it is possible to code fairly complex applications without using pointers but 
most likely you will have to learn about them and use a couple.




More information about the Python-list mailing list