Nathaniel Smith <n...@pobox.com> writes: >> > tmp1 = a + b >> > tmp1 += c >> > tmp1 /= c >> > result = tmp1 >> >> Could this transformation be done in the ast? And would that help? > > I don't think it could be done in the ast because I don't think you can > work with anonymous temporaries there. But, now that you mention it, it > could be done on the fly in the implementation of the relevant opcodes. > I.e., BIN_ADD could do > > if (Py_REFCNT(left) == 1) > result = PyNumber_InPlaceAdd(left, right); > else > result = PyNumber_Add(left, right) > > Upside: all packages automagically benefit! > > Potential downsides to consider: > - Subtle but real and user-visible change in Python semantics. I'd be a > little nervous about whether anyone has implemented, say, an iadd with side > effects such that you can tell whether a copy was made, even if the object > being copied is immediately destroyed. Maybe this doesn't make sense > though.
Hmm. I don't think this is as unlikely as it may sound. Consider eg the h5py module: with h5py.File('database.h5') as fh: result = fh['key'] + np.ones(42) if this were transformed to with h5py.File('database.h5') as fh: tmp = fh['key'] tmp += np.ones(42) result = tmp then the database.h5 file would get modified, *and* result would be of type h5py.Dataset rather than np.array. Best, -Nikolaus -- GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F »Time flies like an arrow, fruit flies like a Banana.« _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com