variable-assignment

Assignment with “or” in python

♀尐吖头ヾ 提交于 2019-12-05 08:39:26
问题 Is it considered bad style to assign values to variables like this? x = "foobar" or None y = some_variable or None In the above example, x gets the value 'foobar'. 回答1: No, it's a common practice. It's only considered bad style for expressions that are considerably longer than yours. 回答2: The primary danger of doing something like this is the possibility that (in the second case) some_variable is False but not None (the integer 0 , for instance) and you don't want to end up with y equal to

Why use a closure for assignment instead of directly assigning a value to a key?

本秂侑毒 提交于 2019-12-05 08:09:32
I was watching this video and at 7:10 he's adding a db dependency and is using a closure to assign the value. My question is why not just use direct assignment instead, I mean isn't doing this: $container['db'] = $capsule; equivalent to doing this: $container['db'] = function ($container) use ($capsule) { return $capsule; } If not, what is the difference and which way is better? TLDR it's because defining dependencies as closures makes it possible for dependency injection container to build them on demand, hence you don't need to worry about their order of definition and managing their

Variable assignment in lambda expression

穿精又带淫゛_ 提交于 2019-12-05 08:02:08
问题 I have the following fragment of code: SomeClass someClass; switch (type) { case FIRST: someClass = new SomeClass(); break; case SECOND: OptionalLong optional = findSomeOptional(); optional.ifPresent(value -> someClass = new SomeClass(value)); } And I'm trying to assign new object to someClass reference in lambda expresion but then I've got error message: "variable used in lambda should be effectively final" . When I add final to declaration of someClass I got another error "cannot assign

Javascript increment while assigning

℡╲_俬逩灬. 提交于 2019-12-05 07:50:53
I was having a conversation about the prefix increment operator, and we seem to have run into a disagreement. When running this code: var x = 0; x = ++x; is the second line equivalent to: x = (x = x + 1) OR x = (x + 1) It is hard to tell the difference because the results are identical (both result in x having a value of 1) I believe that the value is not saved to the original variable when the left hand side of the assignment is the variable itself. My counterpart disagrees and thinks the value is saved to the original variable whenever the ++ operator is used. Which one of us is right? It is

Julia: Assignment in Arrays

给你一囗甜甜゛ 提交于 2019-12-05 07:45:43
When indexing more than one level for an array, it works fine. But when I used it to assign values, it did not. Does anyone know why A does not change below? In [4]: A = rand(6) Out [4]: 6-element Array{Float64,1}: 0.111552 0.155126 0.78485 0.147477 0.362078 0.959022 In [5]: A[3:5][[true,false,true]] Out [5]: 2-element Array{Float64,1}: 0.78485 0.362078 In [6]: A[3:5][[true,false,true]] = [99, 999] Out [6]: 2-element Array{Int64,1}: 99 999 In [7]: A Out [7]: 6-element Array{Float64,1}: 0.111552 0.155126 0.78485 0.147477 0.362078 0.959022 This is because indexing arrays by ranges and vectors

Object property assignment with destructuring?

旧巷老猫 提交于 2019-12-05 06:54:12
I'd like to use ES6 destructuring to assign properties of an object, but can't figure out the syntax. <= ES5: var dst = {}; // already in existence, with its own props, methods, etc. var src = { a: 'foo', b: 'bar', c: 'baz' }; dst.a = src.a; dst.b = src.b; >= ES6 (my own made-up, not-working syntax): let dst = {}; let src = { a: 'foo', b: 'bar', c: 'baz' }; dst[{a, b}] = src; Is it possible to use destructuring assignment onto an object? What's the correct syntax? EDIT: In my use case, dst is an object that existed well before needing to merge a subset of src 's properties; it is not a new

Cell assignment of a 2-dimensional Matrix in Python, without numpy

☆樱花仙子☆ 提交于 2019-12-05 06:45:17
Below is my script, which basically creates a zero matrix of 12x8 filled with 0. Then I want to fill it in, one by one. So lets say column 2 row 0 needs to be 5. How do I do that? The example below shows how I did it and the wrong (for my needs) output: list_MatrixRow = [] list_Matrix = [] #Not to be confused by what the book calls, optimal alignment score matrix int_NumbOfColumns = 12 int_NumbOfRows = 8 for i in range (0, int_NumbOfColumns): # Puts Zeros across the first Row list_AlignMatrixRow.append(0) for i in range (0, int_NumbOfRows): list_AlignMatrix.append(list_AlignMatrixRow) #add the

error: cannot assign a value to final variable

前提是你 提交于 2019-12-05 04:23:28
I am working on an assignment and I'm stuck on this error: cannot assign a value to final variable count Here is my code so far... public class List { private final int Max = 25; private final int count; private Person list[]; public List() { count = 0; list = new Person[Max]; } public void addSomeone(Person p) { if (count < Max){ count++; // THIS IS WHERE THE ERROR OCCURS list[count-1] = p; } } public String toString() { String report = ""; for (int x=0; x < count; x++) report += list[x].toString() + "\n"; return report; } } I'm very new to java and am obviously not a computer whiz so please

Function `[<-` will _replace_ an element, but not append an _element_

纵然是瞬间 提交于 2019-12-05 04:12:49
I noticed the following when using '[<-' . I am successful at replacing elements but not at appending an element to the vector. Example: VarX <- integer() VarX[1] <- 11 `[<-`(VarX, 2, 22) VarX # [1] 11 # Expected the value of VarX to be: # [1] 11 22 # Also tried: `[<-`(VarX, i=2, value=22) VarX # [1] 11 However, if there is already a value at the index, the value does get replaced. VarX <- integer() VarX[1] <- 11 VarX[2] <- 99 VarX # [1] 11 99 `[<-`(VarX, 2, 22) VarX # [1] 11 22 Do I simply have the syntax wrong, or is this as intended? Any further insight into what is going on here would be

updating references in an expression with a nested assignment

為{幸葍}努か 提交于 2019-12-05 04:03:45
Looking at this example code similar to this question : public class A { public static void main(String args[]){ A a = new A(); System.out.println(a.equals((a = null))); } } This prints false. Why doesn't it fail with a NullPointerException? The assignment has to get processed before the equals method can run, but somehow that doesn't affect the reference that equals is called on until after the whole line is evaluated? I didn't see where in the Java language spec it describes this, did I miss it somewhere? ζ-- From JLS : At run time, method invocation requires five steps. First, a target