Skip to content
Advertisement

Tag: syntax

How is __mro__ different from other double underscore names?

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 instance. However, __mro__ can only be accessed via Class name and

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 Both 3. and 4. should be

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 separation of arguments: The first line seems to simply ignore the

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: 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 evaluating an assignment, the right-hand side is evaluated before the left-hand

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 list of attributes manually. Is it possible to make a python class iterable itself using the standard syntax? That is, I’d like to be able

Advertisement