On Thu, Aug 19, 2010 at 9:12 AM, Thomas Jollans <tho...@jollybox.de> wrote:
> I doubt any actual Python implementation optimizes this -- how could it? > The > object "set" is clearly being called twice, and it happens to be called > with > the object "a" as a sole argument twice. What if "set" has side effects? A > compiler could only exclude this possibility if it knew exactly what "set" > will be at run time, which it can't. > > I expect that "set" and "a" have to be looked up twice, actually: > "set(a).union(b)" might rebind either one of them. This would be considered > a > very rude and inappropriate thing to do, but Python usually guarantees to > allow bad taste and behaviour. > > Yep. >>> def test(): ... a = [1] ... b = [1] ... if set(a).union(b) == set(a): pass ... >>> dis.dis(test) 2 0 LOAD_CONST 1 (1) 3 BUILD_LIST 1 6 STORE_FAST 0 (a) 3 9 LOAD_CONST 1 (1) 12 BUILD_LIST 1 15 STORE_FAST 1 (b) 4 18 LOAD_GLOBAL 0 (set) 21 LOAD_FAST 0 (a) 24 CALL_FUNCTION 1 27 LOAD_ATTR 1 (union) 30 LOAD_FAST 1 (b) 33 CALL_FUNCTION 1 36 LOAD_GLOBAL 0 (set) 39 LOAD_FAST 0 (a) 42 CALL_FUNCTION 1 45 COMPARE_OP 2 (==) 48 JUMP_IF_FALSE 4 (to 55) 51 POP_TOP 52 JUMP_FORWARD 1 (to 56) >> 55 POP_TOP >> 56 LOAD_CONST 0 (None) 59 RETURN_VALUE > I might be wrong on some points here, but this is what I expect the > expression > (set(a).union(b) == set(a)) has to do, in any conforming implementation of > Python. Please correct me if I'm wrong. > You can use dis module to let Python do compiling and explaining for you -- With best regards, Daniel Kluev
-- http://mail.python.org/mailman/listinfo/python-list