[Python-Dev] pyc files, constant folding and borderline portability issues (original) (raw)
Alexandru Moșoi brtzsnr at gmail.com
Tue Apr 7 20:59:01 CEST 2009
- Previous message: [Python-Dev] Shorter float repr in Python 3.1?
- Next message: [Python-Dev] pyc files, constant folding and borderline portability issues
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: "Cesare Di Mauro" <cesare.dimauro at a-tono.com> So if Python will generate
LOADCONST 1 LOADCONST 2 BINARYADD the constant folding code will simply replace them with a single LOADCONST 3 When working with such kind of optimizations, the temptation is to apply them at any situation possible. For example, in other languages this a = b * 2 * 3 will be replaced by a = b * 6 In Python I can't do that, because b can be an object which overloaded the * operator, so it must be called two times, one for 2 and one for 3.
Not necessarily. For example C/C++ doesn't define the order of the operations inside an expression (and AFAIK neither Python) and therefore folding 2 * 3 is OK whether b is an integer or an arbitrary object with mul operator overloaded. Moreover one would expect * to be associative and commutative (take a look at Python strings); if a * 2
- 3 returns a different result from a * 6 I will find it very surprising and probably reject such code.
However you can fix the order of operations like this:
a = (b * 2) * 3
or
a = b * (2 * 3)
or
a = b * 2 a = a * 3
-- Alexandru Moșoi http://alexandru.mosoi.googlepages.com
- Previous message: [Python-Dev] Shorter float repr in Python 3.1?
- Next message: [Python-Dev] pyc files, constant folding and borderline portability issues
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]