Posts: 69 Threads: 13 Joined: Jun 2024 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 Posts: 4,874 Threads: 78 Joined: Jan 2018 (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 » Posts: 69 Threads: 13 Joined: Jun 2024 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 Posts: 18 Threads: 7 Joined: Mar 2025 (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. Posts: 69 Threads: 13 Joined: Jun 2024 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 Posts: 6,920 Threads: 22 Joined: Feb 2020 Jul-09-2025, 04:35 AM (This post was last modified: Jul-09-2025, 01:15 PM by deanhystad.) 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. Posts: 2,171 Threads: 12 Joined: May 2017 Jul-09-2025, 05:22 AM (This post was last modified: Jul-09-2025, 05:23 AM by DeaD_EyE.) (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() Posts: 6,920 Threads: 22 Joined: Feb 2020 Jul-09-2025, 01:33 PM (This post was last modified: Jul-10-2025, 12:03 AM by deanhystad.) 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. Posts: 69 Threads: 13 Joined: Jun 2024 deanhystad thanks, An excellent explanation as usual. curbie Posts: 69 Threads: 13 Joined: Jun 2024 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. |