Article of interest: Python pros/cons for the enterprise

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Feb 22 11:13:50 EST 2008


On Fri, 22 Feb 2008 04:48:28 -0800, Nicola Musatti wrote:

> 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.

Aaah, that's much nicer and easier to understand than the list
comprehension.  After this great example I'll switch to C++.  ;-)

But somehow you still manage memory by writing in a style that favors
value types.

SCNR,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list