variable-assignment

Python initialize multiple variables to the same initial value

Python initialize multiple variables to the same initial value Question: I have gone through these questions, Python assigning multiple variables to same value? list behavior concerned with tuples, I want just variables may be a string, integer or dictionary More elegant way of declaring multiple variables at the same time The question has something I …

Total answers: 7

Why does foo = filter(…) return a <filter object>, not a list?

Why does foo = filter(…) return a <filter object>, not a list? Question: Working in Python IDLE 3.5.0 shell. From my understanding of the builtin “filter” function it returns either a list, tuple, or string, depending on what you pass into it. So, why does the first assignment below work, but not the second (the …

Total answers: 5

Python Multiple Assignment Statements In One Line

Python Multiple Assignment Statements In One Line Question: (Don’t worry, this isn’t another question about unpacking tuples.) In python, a statement like foo = bar = baz = 5 assigns the variables foo, bar, and baz to 5. It assigns these variables from left to right, as can be proved by nastier examples like >>> …

Total answers: 5

Python Assignment Operator Precedence – (a, b) = a[b] = {}, 5

Python Assignment Operator Precedence – (a, b) = a[b] = {}, 5 Question: I saw this Python snippet on Twitter and was quite confused by the output: >>> a, b = a[b] = {}, 5 >>> a {5: ({…}, 5)} What is going on here? Asked By: Andrew || Source Answers: From the Assignment statements …

Total answers: 1

Why isn't assigning to an empty list (e.g. [] = "") an error?

Why isn't assigning to an empty list (e.g. [] = "") an error? Question: In python 3.4, I am typing [] = “” and it works fine, no Exception is raised. Though of course [] is not equal to “” afterwards. [] = () also works fine. “” = [] raises an exception as expected …

Total answers: 2

Weird lambda behaviour in loops

Weird lambda behaviour in loops Question: I stumbled upon a behaviour in python that I have a hard time understanding. This is the proof-of-concept code: from functools import partial if __name__ == ‘__main__’: sequence = [‘foo’, ‘bar’, ‘spam’] loop_one = lambda seq: [lambda: el for el in seq] no_op = lambda x: x loop_two = …

Total answers: 2

Python Assign value to variable during condition in while Loop

Python Assign value to variable during condition in while Loop Question: A simple question about Python syntax. I want to assign a value from a function to a variable during the condition for a while loop. When the value returned from the function is false, the loop should break. I know how to do it …

Total answers: 4

Local variable referenced before assignment?

Local variable referenced before assignment? Question: I am using the PyQt library to take a screenshot of a webpage, then reading through a CSV file of different URLs. I am keeping a variable feed that incremements everytime a URL is processed and therefore should increment to the number of URLs. Here’s code: webpage = QWebPage() …

Total answers: 5