Article of interest: Python pros/cons for the enterprise
Nicola Musatti
nicola.musatti at gmail.com
Fri Feb 22 07:48:28 EST 2008
On Feb 22, 12:07 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Nicola Musatti <nicola.musa... at gmail.com> writes:
> > In C++ memory is just another resource which you can handle just like
> > any other one, possibly using RAII.
>
> Ok, I'll bite. Here's a straightforward Python expression:
>
> a = [f(x) + g(y) for x,y in izip(m1, m2) if h(x,y).frob() == 7]
>
> Consider how many intermediate objects are being allocated in figuring
> out that listcomp. Do you REALLY want to manage all the deallocation
> with something like RAII?
What makes you think that a translation of a similar expression would
involve explicit dynamic allocation at all? Barring bugs, here's an
equivalent example:
#include <iostream>
#include <map>
#include <vector>
int f(int n) { return n * 2; }
int g(int n) { return ( n * 2 ) + 1; }
std::map<int, int> izip(int i, int j) {
std::map<int, int> m;
m[i] = j;
m[j] = i;
return m;
}
class A {
int i, j;
public:
A(int ii, int jj) : i(ii), j(jj) {}
int frob() { return i + j; }
};
A h(int i, int j) { return A(i, j); }
int main() {
int m1 = 3;
int m2 = 4;
std::vector<int> a;
std::map<int, int> m = izip(m1, m2);
for ( std::map<int,int>::iterator i = m.begin(); i != m.end(); ++i )
{
if ( h(i->first, i->second).frob() == 7 )
a.push_back(f(i->first) + g(i->second));
}
for ( std::vector<int>::iterator i = a.begin(); i != a.end(); ++i )
std::cout << *i << '\n';
}
As you can see the standard library takes care of all memory
management.
Cheers,
Nicola Musatti
More information about the Python-list
mailing list