Movatterモバイル変換


[0]ホーム

URL:


[Python-Dev] [numpy wishlist] Interpreter support for temporaryelision in third-party classes

Nikolaus RathNikolaus at rath.org
Fri Jun 6 04:27:20 CEST 2014


Nathaniel Smith <njs at 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 theh5py module:with h5py.File('database.h5') as fh:     result = fh['key'] + np.ones(42)if this were transformed towith h5py.File('database.h5') as fh:    tmp = fh['key']    tmp += np.ones(42)    result = tmpthen the database.h5 file would get modified, *and* result would be oftype h5py.Dataset rather than np.array.Best,-Nikolaus-- GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599FFingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F             »Time flies like an arrow, fruit flies like a Banana.«


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2025 Movatter.jp