Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python logic absurdity
#1
I’ve run into Python’s logic absurdity twice now, the first time I wrote it off to Python “magic”, though I hated it, I got through it as a one-off short-cut. This time, I can’t get though it, and the notion of programming “magic” is anathema to me.

The first was in a swap endian routine:
 blk = bytearray(512) # start new block for i in range(0, BLOCK, 2): # swap endian for entire block loop, step by 2 (word) blk[i], blk[i + 1] = block[i + 1], block[i] # swap even byte to odd byte (intermediate swap done by python???) return blk # return bytearray endian-swapped block
A buddy of mine who first steered to python suggested this code correction without the swap variable I had.

Now this from a RE tutorial:
# Precompile the patterns regexes = [ re.compile(p) # more logic absurdity, must be python magic for p in ['this', 'that'] # first check "this" then "that" ]
Reference the variable “p” before it is loaded, they both worked either way, and therein is my question:
Is there an easy way to understand when sort-cuts apply?

If the answer is use and experience, that's ok, as long as they still work after I rewrite them to make sense me while learning.

curbie
Reply
#2
(Jul-08-2025, 07:57 PM)Curbie Wrote: # more logic absurdity, must be python magic
There is nothing magic in this, it is a well known syntax of python called "list comprehension". Roughly speaking
L = [x for x in spam if ham(x)]
is equivalent to
L = [] for x in spam: if ham(x): L.append(x)
Learn about "list comprehensions" and also "generator expressions" in Python.

If Python is new to you, learn with patience.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Gribouillis thanks for the reply,

I’ve never tried to hide the fact that I’m a newbee, coming from lower level languages, the “magic” you referred to had to do with referencing the variable “p” before it is loaded:
regexes = [ re.compile(p)	# this references “p” before the next line loads it for p in ['this', 'that'] ]
also swapping the values of x and y without needing a temporary variable. E.G.:
x, y = y, x

I don’t understand how this these are explained by "list comprehension". I took w3schools online tutorial and have gone though it several times which includes, https://www.w3schools.com/python/python_...ension.asp
If you can recommend another on-line full python tutorial, I’ll take that one also.

curbie
Reply
#4
(Jul-09-2025, 12:11 AM)Curbie Wrote: Gribouillis thanks for the reply,

I’ve never tried to hide the fact that I’m a newbee, coming from lower level languages, the “magic” you referred to had to do with referencing the variable “p” before it is loaded:
regexes = [ re.compile(p)	# this references “p” before the next line loads it for p in ['this', 'that'] ]
also swapping the values of x and y without needing a temporary variable. E.G.:
x, y = y, x

I don’t understand how this these are explained by "list comprehension". I took w3schools online tutorial and have gone though it several times which includes, https://www.w3schools.com/python/python_...ension.asp
If you can recommend another on-line full python tutorial, I’ll take that one also.

curbie

Coming from low level languages, I can understand the confusion. However, you have remember that Python is a high level language. Both of these are in the realm of syntactic sugar (IMHO). The compiler parses the statements and does the right thing. Internally, there could be a use of a third variable.

Admittedly, I don't know the internals of how the x, y = y, x is done but if I had to reason through it, I would think that the compiler would use a temp variable internally. Think of it like this:
int i = a == b ? a : b;
It's just a special syntax that the compiler understands and does the right thing.
Reply
#5
voidtrance,

What I feel I'm missing, is some cheat-sheet to when a references can be used before the it's loaded, or since it's my understanding that the x and y variables are both objects, is the answer to x, y = y, x that all that's going on here is telling python what is the source and destination, then letting python handle the details.

curbie
Reply
#6
What happens when you do this?
x = 1 + 2
First python evaluates the expression on the right side of the "=" and creates a new object that contains the result.
Second python assigns the result to the variable on the left side of the "=".

The same thing happens with x, y = y, x. First python evaluates he expression on the right side of the "=". If x == 2, and y == 3, the expression "y, x" results in a tuple (3, 2). Next python assigns the variables x, y to the tuple. This unpacks the first value of the tuple into x, and the second value into y.

An important thing to remember about python variables is they are only names. Unlike variables in C++, a variable does not store a value. That's why the same python variable can reference a number or a string or a list. All the variable knows about the object it references is the object id. Python variables are similar to keys in a dictionary. The local and global name spaces act very much like dictionaries, and variables are the keys used to get values stored in those dictionaries.
Reply
#7
(Jul-08-2025, 07:57 PM)Curbie Wrote: The first was in a swap endian routine:

You could use array.array:

import array data = array.array("H", [42] * 256) # H == unsigned short 0-65535, 2 bytes # data has 256 values # each value has 2 bytes # now you can do data.byteswap()
Larz60+ likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Quote:Both of these are in the realm of syntactic sugar (IMHO). The compiler parses the statements and does the right thing. Internally, there could be a use of a third variable.
x, y = y, x works because of what variables are in python, how packing and unpacking works in python, and how python evaluates expressions. It is not "syntactic sugar". These are all important parts of python, and understanding what they do is important to being an effective python programmer.

When evaluating y, x python creates a tuple containing the object referenced by y followed by the object referenced by x. This is packing.
When assigning x, y = result, x is assigned to reference the first object in result and y the second. This is unpacking. Packing and unpacking are important for many reasons, but my favorite is they let you write functions that return multiple values.

Notice there is no need for a "third variable". Prior to assignment, x and y are used to create a new object, a tuple that contains the objects that x and y referred. During assignment, x and y are reassigned to refer to objects in the tuple. At no time does python do x = y or y = x.
Reply
#9
deanhystad thanks,

An excellent explanation as usual.

curbie
Reply
#10
DeaD_EyE thanks,

I've saved array along with the struct library info, (the "H" flag triggered struct memory) while learning python I want to stick with coding my own libraries as much as possible, but I also have the porting of my old dis-assembler queued up, and for that I need to have all these binary handling options evaluated by then.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Logic Error rmfooty 3 2,271 Dec-05-2022, 01:44 PM
Last Post: rmfooty
  logic python JunkBoy 5 3,376 Aug-01-2022, 05:13 AM
Last Post: JunkBoy
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 2,814 Sep-18-2019, 09:43 AM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.