variable-assignment

Python Assign value to variable during condition in while Loop

纵然是瞬间 提交于 2019-12-18 04:31:43
问题 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 in PHP. while (($data = fgetcsv($fh, 1000, ",")) !== FALSE) However when I try a similar syntax in Python I get a syntax error. 回答1: You cannot use assignment in an expression. Assignment is itself a statement, and you cannot combine Python statements. This is an

Shortest way to check for null and assign another value if not

爱⌒轻易说出口 提交于 2019-12-17 22:12:09
问题 I am pulling varchar values out of a DB and want to set the string I am assigning them to as "" if they are null . I'm currently doing it like this: if (string.IsNullOrEmpty(planRec.approved_by) == true) this.approved_by = ""; else this.approved_by = planRec.approved_by.toString(); There seems like there should be a way to do this in a single line something like: this.approved_by = "" || planRec.approved_by.toString(); However I can't find an optimal way to do this. Is there a better way or

Addition and Subtraction Assignment Operator With Sequelize

江枫思渺然 提交于 2019-12-17 21:15:25
问题 I would like to do an update by doing a simple addition on Sequelize. table: id || data 1 || 10 sample: db.table.update({ data : 1 }, { where: { id: 1 }}); after this query id || data 1 || 11 I know it's a simple question, but I could not find the solution. Which operator can I add and subtract? Thank you 回答1: Here it is : db.table.update({ field: Sequelize.literal('data + 1') }, { where: { id: 1 }})) OR User.findById(1).then(user => { // -----> First Way return user.increment('my-integer

Python: setting two variable values separated by a comma in python

本小妞迷上赌 提交于 2019-12-17 21:06:58
问题 What is the difference in python between doing: a, b = c, max(a, b) and a = c b = max(a, b) what does having the two variable assignments set on the same line do? 回答1: Your two snippets do different things: try with a , b and c equal to 7 , 8 and 9 respectively. The first snippet sets the three variables to 9 , 8 and 9 . In other words, max(a, b) is calculated before a is assigned to the value of c . Essentially, all that a, b = c, max(a, b) does is push two values onto the stack; the

What does “||=” do in Ruby 1.9.2? [duplicate]

帅比萌擦擦* 提交于 2019-12-17 20:17:03
问题 This question already has answers here : What does ||= (or-equals) mean in Ruby? (22 answers) Closed 5 years ago . params[:user][:role_ids] ||= [] What does it do? ruby -v = 1.9.2p290 回答1: It assigns [] to params["user][:role_ids] if params["user][:role_ids] is nil or another falsy value... Otherwise, it retains the original value of params["user][:role_ids] Example variable = nil variable ||= "string" puts variable # "string" variable2 = "value" variable2 ||= "string" puts variable2 # "value

Zend Studio reports warning: Assignment in condition. Is this so bad?

戏子无情 提交于 2019-12-17 20:07:22
问题 I have recently started using Zend Studio which has reported as warning the following type of code: $q = query("select * from some_table where some_condition"); while ($f = fetch($q)) { // some inner workings } To stop the warning the code needs to be written like this: $q = query("select * from some_table where some_condition"); $f = fetch($q); while ($f) { // some inner workings $f = fetch($q); } Why is this marked as a warning? Is it so bad? I understand that the warning may be designed to

Object assignment and pointers

与世无争的帅哥 提交于 2019-12-17 19:46:13
问题 I am a little confused about object assignment and pointers in Ruby, and coded up this snippet to test my assumptions. class Foo attr_accessor :one, :two def initialize(one, two) @one = one @two = two end end bar = Foo.new(1, 2) beans = bar puts bar puts beans beans.one = 2 puts bar puts beans puts beans.one puts bar.one I had assumed that when I assigned bar to beans, it would create a copy of the object, and modifying one would not affect the other. Alas, the output shows otherwise. ^_^

var vs := in Go

落花浮王杯 提交于 2019-12-17 19:33:13
问题 In the Go web server example here: http://golang.org/doc/effective_go.html#web_server The following line of code works var addr = flag.String("addr", ":1718", "http service address") but changing it to addr := flag.String("addr", ":1718", "http service address") is a compilation error. Why? Does it have anything to do with the face that the return type of the function is *string instead of string ? What difference does that make? UPDATE : Thanks for pointing out that := is not allowed at the

JavaScript variable undefined vs not defined

蹲街弑〆低调 提交于 2019-12-17 19:28:11
问题 I have an HTML page with the following JavaScript attached. alert(box); box = "Thinking outside the box"; In the console I get "Uncaught ReferenceError: box is not defined" when I change it to: alert(box); var box = "Thinking outside the box"; The alert gets called and shows undefined. I need to be able to explain this, I have a vague idea of why this happens. I know that when I use var, JavaScript knows the variable is there before the alert is executed, but has not necessarily assigned a

The use of “this” in Java

不打扰是莪最后的温柔 提交于 2019-12-17 19:24:44
问题 If I write the following class: public class Example { int j; int k; public Example(int j, int k) { j = j; k = k; } public static void main(String[] args) { Example exm = new Example(1,2); System.out.println(exm.j); System.out.println(exm.k); } } The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write: this.j = j; this.k = k; But if I don't write it,