variable-assignment

Local variable referenced before assignment in Python?

烈酒焚心 提交于 2019-11-26 08:20:22
问题 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() fo = open(\"C:/Users/Romi/Desktop/result1.txt\", \"w\") feed = 0 def onLoadFinished(result): #fo.write( column1[feed])#, column2[feed], urls[feed]) #feed = 0 if not result: print \"Request failed\" fo.write(column1

Assignment inside lambda expression in Python

对着背影说爱祢 提交于 2019-11-26 07:56:58
问题 I have a list of objects and I want to remove all objects that are empty except for one, using filter and a lambda expression. For example if the input is: [Object(name=\"\"), Object(name=\"fake_name\"), Object(name=\"\")] ...then the output should be: [Object(name=\"\"), Object(name=\"fake_name\")] Is there a way to add an assignment to a lambda expression? For example: flag = True input = [Object(name=\"\"), Object(name=\"fake_name\"), Object(name=\"\")] output = filter( (lambda o: [flag or

Linux bash: Multiple variable assignment

北慕城南 提交于 2019-11-26 06:57:05
问题 Does exist in linux bash something similar to the following code in PHP: list($var1, $var2, $var3) = function_that_returns_a_three_element_array() ; i.e. you assign in one sentence a corresponding value to 3 different variables. Let\'s say I have the bash function myBashFuntion that writes to stdout the string \"qwert asdfg zxcvb\". Is it possible to do something like: (var1 var2 var3) = ( `myBashFuntion param1 param2` ) The part at the left of the equal sign is not valid syntax of course. I\

Why does C++ allow an integer to be assigned to a string?

冷暖自知 提交于 2019-11-26 06:43:33
问题 I encountered an interesting situation today in a program where I inadvertantly assigned an unsigned integer to a std::string. The VisualStudio C++ compiler did not give any warnings or errors about it, but I happened to notice the bug when I ran the project and it gave me junk characters for my string. This is kind of what the code looked like: std::string my_string(\"\"); unsigned int my_number = 1234; my_string = my_number; The following code also compiles fine: std::string my_string(\"\")

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

拥有回忆 提交于 2019-11-26 06:31:18
问题 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 though, () = \"\" raises an exception as expected though. So, what\'s going on? 回答1: You are not comparing for equality. You are assigning . Python allows you to assign to multiple targets: foo, bar = 1, 2 assigns the two values to foo and bar , respectively. All you need is a sequence or

Why is `a = a` `nil` in Ruby?

我的梦境 提交于 2019-11-26 05:59:01
问题 I watched this video. Why is a = a evaluated to nil if a is not defined? a = a # => nil b = c = q = c # => nil 回答1: Ruby interpreter initializes a local variable with nil when it sees an assignment to it. It initializes the local variable before it executes the assignment expression or even when the assignment is not reachable (as in the example below). This means your code initializes a with nil and then the expression a = nil will evaluate to the right hand value. a = 1 if false a.nil? # =>

Destructuring assignment in JavaScript

流过昼夜 提交于 2019-11-26 05:37:45
问题 As can be seen in the Mozilla changlog for JavaScript 1.7 they have added destructuring assignment. Sadly I\'m not very fond of the syntax (why write a and b twice?): var a, b; [a, b] = f(); Something like this would have been a lot better: var [a, b] = f(); That would still be backwards compatible. Python-like destructuring would not be backwards compatible. Anyway the best solution for JavaScript 1.5 that I have been able to come up with is: function assign(array, map) { var o = Object();

Global variables in R

人走茶凉 提交于 2019-11-26 05:18:09
问题 I am poking into the manuals, I wanted to ask the community: How can we set global variables inside a function? 回答1: As Christian's answer with assign() shows, there is a way to assign in the global environment. A simpler, shorter (but not better ... stick with assign) way is to use the <<- operator, ie a <<- "new" inside the function. 回答2: I found a solution for how to set a global variable in a mailinglist posting via assign: a <- "old" test <- function () { assign("a", "new", envir =

Multiple assignment semantics

核能气质少年 提交于 2019-11-26 04:44:57
问题 In Python one can do: a, b = 1, 2 (a, b) = 1, 2 [a, b] = 1, 2 I checked the generated bytecode using dis and they are identical. So why allow this at all? Would I ever need one of these instead of the others? 回答1: One case when you need to include more structure on the left hand side of the assignment is when you're asking Python unpack a slightly more complicated sequence. E.g.: # Works >>> a, (b, c) = [1, [2, 3]] # Does not work >>> a, b, c = [1, [2, 3]] Traceback (most recent call last):

Indirect variable assignment in bash

大兔子大兔子 提交于 2019-11-26 03:30:09
问题 Seems that the recommended way of doing indirect variable setting in bash is to use eval : var=x; val=foo eval $var=$val echo $x # --> foo The problem is the usual one with eval : var=x; val=1$\'\\n\'pwd eval $var=$val # bad output here (and since it is recommended in many places, I wonder just how many scripts are vulnerable because of this...) In any case, the obvious solution of using (escaped) quotes doesn\'t really work: var=x; val=1\\\"$\'\\n\'pwd\\\" eval $var=\\\"$val\\\" # fail with