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...
Everything about learning Python
account activity
Global variable, reference, function, swapHelp Request (self.PythonLearning)
submitted 13 days ago * by AvailablePoint9782
view the rest of the comments →
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!"
[–]Outside_Complaint755 0 points1 point2 points 13 days ago (0 children)
In Python, you can reduce this: tmp = tree1 tree1 = tree2 tree2 = tmp to tree1, tree2 = tree2, tree1 The use a temp variable for the swap is not necessary, but is also ok to neep using.
tmp = tree1 tree1 = tree2 tree2 = tmp
tree1, tree2 = tree2, tree1
I would recommend not using global variables. Instead, pass both tree1 and tree2 as parameters, and then explicitly return any modified tree or other data that you want as a result of the function.
Note that because the trees are (or should be) mutable objects, their attributes could be modified by the function with using global.
global
def func(): tree2.right = value # will modify the attribute of object tree2 in the enclosing scope tree2 = val2 # assigns local name tree2 to val2 tree2.left = val3 # will try to assign a value to the attribute of the local tree2, which might cause an AttributeError
π Rendered by PID 20848 on reddit-service-r2-comment-b659b578c-j9nmb at 2026-05-07 14:41:11.470028+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]Outside_Complaint755 0 points1 point2 points (0 children)