On 08.12.2012 15:18, Stefan Schwarzer wrote:
On 2012-12-08 11:35, Mike Müller wrote:
Generell würde ich `exec` nur in sehr seltenen Fällen nehmen. Es gibt fast immer eine andere, oft bessere, Lösung.
das dachte ich auch - und denke ich tendenziell immer noch. Ich war aber überrascht, als ich neulich im `collections`-Modul sah, dass für `namedtuple` eine nicht ganz kleine Klasse mit `exec` erzeugt wird
Die Verwendung von exec für "namedtuple" ist eine Geschwindigkeits-Optimierung, und die sind ja oft nicht hübsch. Es gibt dazu auch ein (rejected) Ticket im Python-Roundup mit einer kurzen Diskussion. http://bugs.python.org/issue3974 Ich habe das mal nachgemessen und mit der Implementierung ohne "exec" verglichen. Die Definierung der neuen namedtuple-Klasse ist dadurch zwar schneller aber die Instanzierung deutlich langsamer. Chris