variable-assignment
Compressing `x if x else y` statement in Python
There is no way to do this, and that’s intentional. The ternary if is only supposed to be used for trivial cases. If you want to use the result of a computation twice, put it in a temporary variable: value = info.findNext(“b”) value = value if value else “Oompa Loompa” Once you do this, it … Read more
Compressing `x if x else y` statement in Python
There is no way to do this, and that’s intentional. The ternary if is only supposed to be used for trivial cases. If you want to use the result of a computation twice, put it in a temporary variable: value = info.findNext(“b”) value = value if value else “Oompa Loompa” Once you do this, it … Read more
Python: Assign Value if None Exists
You should initialize variables to None and then check it: var1 = None if var1 is None: var1 = 4 Which can be written in one line as: var1 = 4 if var1 is None else var1 or using shortcut (but checking against None is recommended) var1 = var1 or 4 alternatively if you will … Read more
What is the difference between i++ and ++i in C#?
The typical answer to this question, unfortunately posted here already, is that one does the increment “before” remaining operations and the other does the increment “after” remaining operations. Though that intuitively gets the idea across, that statement is on the face of it completely wrong. The sequence of events in time is extremely well-defined in … Read more
Why don’t Java’s +=, -=, *=, /= compound assignment operators require casting long to int?
As always with these questions, the JLS holds the answer. In this case §15.26.2 Compound Assignment Operators. An extract: A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. An example cited from §15.26.2 […] the following code is … Read more
var vs := in Go
In Go, top-level variable assignments must be prefixed with the var keyword. Omitting the var keyword is only allowed within blocks. package main var toplevel = “Hello world” // var keyword is required func F() { withinBlock := “Hello world” // var keyword is not required }
Fixing the ‘Use of unassigned local variable’ with a null assignment. Why? [duplicate]
When you assign null to the variable you’re telling the compiler to back off because you know better than him so he should not complain about this. This is probably due to the fact that assigning null is considered to imply an explicit action by the developer.
“Expression is not assignable” — Problem assigning float as sum of two other floats in Xcode?
The other answers don’t exactly explain what’s going on here, so this is the basic problem: When you write blackKey.center.x, the blackKey.center and center.x both look like struct member accesses, but they’re actually completely different things. blackKey.center is a property access, which desugars to something like [blackKey center], which in turn desugars to something like … Read more
What is the result of i == (i = 2)?
The behaviour of a C program that executes the expression i == (i = 2) is undefined. It comes from C11 6.5p22: If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar … Read more