In Python, a variable or literal followed by a hanging comma is a one-tuple: …and a series of comma-separated variables/literals (whether or not they are followed by a hanging comma) is also a tuple: However, inside a callable/function, this syntax is treated differently, because the comma is used for separation of arguments: The first line seems to simply ignore the
Tag: syntax
Inline for loop
I’m trying to learn neat pythonic ways of doing things, and was wondering why my for loop cannot be refactored this way: I tried replacing the for loop with: But it doesn’t work. The for v in vm: loop evicts numbers from vm based on when they come next in q. Answer What you are using is called a list
Python “raise from” usage
What’s the difference between raise and raise from in Python? which yields and which yields Answer The difference is that when you use from, the __cause__ attribute is set and the message states that the exception was directly caused by. If you omit the from then no __cause__ is set, but the __context__ attribute may be set as well, and
Is there a standardized method to swap two variables in Python?
In Python, I’ve seen two variable values swapped using this syntax: left, right = right, left Is this considered the standard way to swap two variable values or is there some other means by which …
“except” statement with same exception class as parameter twice
In Python, how can I use except block with same exception name twice in try/except statements without need to wrap code into one more try/except block? Simple example (here each call of pages.get may raise the exception): For now, in my Django app I do handling like this (but I don’t want “extra” try block here): Any handling code better
Extracting specific columns in numpy array
This is an easy question but say I have an MxN matrix. All I want to do is extract specific columns and store them in another numpy array but I get invalid syntax errors. Here is the code: It seems like the above line should suffice but I guess not. I looked around but couldn’t find anything syntax wise regarding
Python: Make class iterable
I have inherited a project with many large classes constituent of nothing but class objects (integers, strings, etc). I’d like to be able to check if an attribute is present without needed to define a …
What does the star and doublestar operator mean in a function call?
What does the * operator mean in Python, such as in code like zip(*x) or f(**k)? How is it handled internally in the interpreter? Does it affect performance at all? Is it fast or slow? When is it useful and when is it not? Should it be used in a function declaration or in a call? Answer The single star
What’s the difference between “2*2” and “2**2” in Python?
What is the difference between the following statements? Statement 1: Statement 2: I see no difference. This raises the following question. Why is Statement 1 used if we can use Statement 2? Answer Try: and to see the difference. ** is the operator for “power of”. In your particular operation, 2 to the power of 2 yields the same as