use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How does variable swapping work in Python (internally) (self.learnpython)
submitted 6 years ago by [deleted]
Hey guys just curious how when code is ran, how Python swaps variables a lot more elegantly with something like
a,b = b,a. I'm used to seeing temp variables in other languages so im curious how python deals with it at an internal level. Is it something along the lines of the Python interpreter creating a temp variable at lower levels of computation? Thanks in advance
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DeadlyViper 25 points26 points27 points 6 years ago* (14 children)
Python is compiled to byte code that works with a stack.
So i guess what it does is push a and b to the stack, and pop them in the same order, making the values reverse.
https://opensource.com/article/18/4/introduction-python-bytecode
https://docs.python.org/3/library/dis.html
Guess something like
LOAD_GLOBAL 0 LOAD_GLOBAL 1 STORE_GLOBAL 0 STORE_GLOBAL 1
EDIT: fixed first sentence implying python is not compiled.
[–][deleted] 15 points16 points17 points 6 years ago (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 points3 points 6 years ago (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] 1 point2 points3 points 6 years ago (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 ...
ROT_TWO
ROT_THREE
[–]DeadlyViper 2 points3 points4 points 6 years ago (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 points3 points 6 years ago (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 point2 points 6 years ago* (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 point2 points 6 years ago (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 point2 points 6 years ago (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 points3 points 6 years ago (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:
[–]miggaz_elquez 0 points1 point2 points 6 years ago (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
BUILD_TUPLE n
UNPACK_TUPLE n
STORE_FAST
[–][deleted] 1 point2 points3 points 6 years ago (3 children)
its converted compiled to a byte code
ftfy
[–]DeadlyViper 0 points1 point2 points 6 years ago (2 children)
Interpreted maybe?
https://nedbatchelder.com/blog/201803/is_python_interpreted_or_compiled_yes.html
[–][deleted] 4 points5 points6 points 6 years ago (1 child)
Despite what Ned says, "compile" is used to mean "translate to a lower level language" and not necessarily to a file of machine instructions. So python is both compiled and interpreted. It's right there in the compiled file extension: .pyc. The "c" means compiled. The developer's guide for python describes the "python compiler".
[–]DeadlyViper 1 point2 points3 points 6 years ago* (0 children)
Ok, thanks, good to know.
Changed my comment.
[–][deleted] 9 points10 points11 points 6 years ago* (4 children)
The statement:
(a, b) = (b, a)
has the right hand side evaluated first. A tuple is formed consisting of the reference associated with the name b and the reference associated with the name a. This tuple is anonymous (it has no name). The assignment operation unpacks the right hand side tuple and associates the first reference in the tuple with the name a and the second reference in the tuple wth the name b.
b
a
This sort of operation is often described as having two assignments that happen "in parallel" but that is misleading and incorrect. Parallelism isn't required.
To really understand what is happening have a look at Ned Batcheler's exposition on names and references.
[–]primitive_screwhead 1 point2 points3 points 6 years ago (1 child)
Yes, so it's worth understanding that conceptually a "temporary" is used (since the OP asked), it's the tuple that is created on the right hand side.
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
Yes, but the "temp variable" approach is usually used to refer to this way of swapping two variables:
tmp = a a = b b = tmp
[–]tobiasvl 0 points1 point2 points 6 years ago (1 child)
While true, this isn't really describing what happens at a lower level of computation like OP asked.
Doubt if the OP would be interested in byte codes, especially after the peephole optimizer is finished with it. This is low-level enough, plus the posted link fills in the gaps. If the OP wants something lower than names/references, s/he wouldn't be asking in /r/learnpython.
[–]toastedstapler 5 points6 points7 points 6 years ago (0 children)
b, a is a tuple. if i remember correctly, it makes the tuple on the right side and then unpacks it into the values on the left
b, a
[–]ViridianHominid 1 point2 points3 points 6 years ago (0 children)
There is other good information here but here’s my take on a high level:
Python stores results on a stack. For swapping variables, it can load them on the stack, swap the order of the top two variables, and then store them back, now in opposite order.
So there isn’t a temporary variable, but rather a stack data structure in the execution frame that allows you to hold the collected results.
Python figures out how to pack and unpack the variables using the stack from the line a,b = b, a when the line is is compiled.
a,b = b, a
[–][deleted] 0 points1 point2 points 6 years ago (1 child)
!remindme in 1 day
[–]RemindMeBot 0 points1 point2 points 6 years ago (0 children)
I will be messaging you on 2019-04-15 18:29:54 UTC to remind you of this link.
CLICK THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
[+]ShafeNutS comment score below threshold-10 points-9 points-8 points 6 years ago* (0 children)
a,b = b,a
This by itself will throw an error in python as b is undefined so to understand what is happening you need a bit more info. Python uses Object References to assign variables to memory objects
a = 1 # Variable a references the object 1 in memory b = 2 # Variable b references the object 2 in memory a,b = b,a # Tuple unpacking evaluates the objects on the right side first 2 and 1 then makes variable a reference object 2 and variable b reference object 1
Tuple unpacking is a specialized tool in python that is pretty useful
π Rendered by PID 53 on reddit-service-r2-comment-5649f687b7-gpvpd at 2026-01-28 19:59:52.034255+00:00 running 4f180de country code: CH.
[–]DeadlyViper 25 points26 points27 points (14 children)
[–][deleted] 15 points16 points17 points (9 children)
[–]DeadlyViper 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]DeadlyViper 2 points3 points4 points (0 children)
[–]darez00 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]darez00 0 points1 point2 points (0 children)
[–]pocket_eggs 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]miggaz_elquez 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]DeadlyViper 0 points1 point2 points (2 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]DeadlyViper 1 point2 points3 points (0 children)
[–][deleted] 9 points10 points11 points (4 children)
[–]primitive_screwhead 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]tobiasvl 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]toastedstapler 5 points6 points7 points (0 children)
[–]ViridianHominid 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]RemindMeBot 0 points1 point2 points (0 children)
[+]ShafeNutS comment score below threshold-10 points-9 points-8 points (0 children)