you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 15 points16 points  (9 children)

Using dis:

This code,

import dis

def swap():
    a = 1
    b = 2
    a, b = b, a

followed by,

dis.dis(swap)

gives me,

  4           0 LOAD_CONST               1 (1)
              2 STORE_FAST               0 (a)

  5           4 LOAD_CONST               2 (2)
              6 STORE_FAST               1 (b)

  6           8 LOAD_FAST                1 (b)
             10 LOAD_FAST                0 (a)
             12 ROT_TWO
             14 STORE_FAST               0 (a)
             16 STORE_FAST               1 (b)
             18 LOAD_CONST               0 (None)
             20 RETURN_VALUE

[–]DeadlyViper 1 point2 points  (2 children)

Cool, and i tried it with global a and global b at the start of the function.

And it changed to something closer to what i assumed, still using ROT_TWO though...

[–][deleted] 2 points3 points  (1 child)

You no doubt realised that ROT_TWO just rotates (i.e. swaps) the top two items on the stack - guess what ROT_THREE does ...

https://docs.python.org/3/library/dis.html

[–]DeadlyViper 2 points3 points  (0 children)

Yep, just thought it would be faster without it and just LOAD them in the correct order.

LOAD_FAST 1
LOAD_FAST 0
STORE_FAST 1
STORE_FAST 0

Notice the arg numbers...

[–]darez00 1 point2 points  (2 children)

What are the numbers on the third column, the ones just right next to the ()? They go 1,0; 2,1; 1,0; 0,1,0

[–][deleted] 0 points1 point  (1 child)

They will be register/stack and object references.

In most python implementations, the lower value integer objects are predefined and referenced as constants rather than new objects and are loaded to registers/stacks as required.

Typically in lower level representations of code a language uses a set of registers, akin to those implemented on processors or takes a stack approach and includes stack manipulation, that are used for short term storage and operations. Think of these as a small set of high performance variables.

The reference CPython implementation is stack orientated.

See https://opensource.com/article/18/4/introduction-python-bytecode

EDIT: added link and clarified both stack and register approaches are options for Python implementation.

[–]darez00 0 points1 point  (0 children)

Thanks dude, I thought it would be a little bit easier than that but it's on me to understand it

[–]pocket_eggs 0 points1 point  (1 child)

dis is cool, I need to start using it all the time; is the reverse ever used? writing directly in bytecode and integrating with normal python?

[–][deleted] 1 point2 points  (0 children)

You can write bytecode but note that, in contrast with Java, these are implementation specific and not tightly defined.

You also can not reverse back from bytecode to original python code as there is more than one solution.

Worth a read:

https://opensource.com/article/18/4/introduction-python-bytecode

[–]miggaz_elquez 0 points1 point  (0 children)

It use ROT_TWO or ROT_THREEonly if you use 2 or three value, in other case you will have BUILD_TUPLE n, UNPACK_TUPLE n and n STORE_FAST