I stumbled upon this behavior for double underscore name that I don’t understand: I know about the name mangling for __name, which doesn’t apply for __name__ (more for overloading operator methods). __id__ behaves just like a regular class variable which can be accessed via Class name as well as i…
Tag: syntax
Generator as function argument
Can anyone explain why passing a generator as the only positional argument to a function seems to have special rules? If we have: This works, as expected. This does not work, as expected. This works, as expected This works, but I don’t understand why. Shouldn’t it fail in the same way as 2) Answer…
Comma Syntax: rationale behind a hanging comma in a statement being a SyntaxError
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 s…
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 l…
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__…
Is there a standardized method to swap two variables in Python?
In Python, I’ve seen two variable values swapped using this syntax: Is this considered the standard way to swap two variable values or is there some other means by which two variables are by convention most usually swapped? Answer Python evaluates expressions from left to right. Notice that while evalua…
“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&#…
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…
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 list of attributes manually. Is it possible to make a python class iterable itself using the standard s…
What do the * (star) and ** (double star) operators mean in a function call?
In code like zip(*x) or f(**k), what do the * and ** respectively mean? How does Python implement that behaviour, and what are the performance implications? See also: Expanding tuples into arguments. Please use that one to close questions where OP needs to use * on an argument and doesn’t know it exists…