variable-assignment

What is the difference between slice assignment that slices the whole list and direct assignment?

会有一股神秘感。 提交于 2019-11-26 03:29:23
问题 I see at many places the use of slice assignment for list s. I am able to understand its use when used with (non-default) indices, but I am not able to understand its use like: a_list[:] = [\'foo\', \'bar\'] How is that different from a_list = [\'foo\', \'bar\'] ? 回答1: a_list = ['foo', 'bar'] Creates a new list in memory and points the name a_list at it. It is irrelevant what a_list pointed at before. a_list[:] = ['foo', 'bar'] Calls the __setitem__ method of the a_list object with a slice as

Java - char, int conversions

痴心易碎 提交于 2019-11-26 03:21:33
问题 In Java, the following is allowed: char c = \'A\' + 1; Here, c will hold the value \'B\'. Above, first the expression is evaluated. So \'A\' gets converted to 65, the whole expression evaluates to 66, and then 66 is converted to \'B\' since we are storing the value in a char. The following, however, gives a compile-time error: char c = \'A\'; c = c + 1; What is the explanation for how Java views the expressions differently? By the way, the following works fine too: char c = \'A\'; c++; 回答1:

Creating an array from a text file in Bash

邮差的信 提交于 2019-11-26 03:20:26
问题 A script takes a URL, parses it for the required fields, and redirects its output to be saved in a file, file.txt . The output is saved on a new line each time a field has been found. file.txt A Cat A Dog A Mouse etc... I want to take file.txt and create an array from it in a new script, where every line gets to be its own string variable in the array. So far I have tried: #!/bin/bash filename=file.txt declare -a myArray myArray=(`cat \"$filename\"`) for (( i = 0 ; i < 9 ; i++)) do echo \

Why does a space in a variable assignment give an error in Bash? [duplicate]

空扰寡人 提交于 2019-11-26 02:59:13
问题 This question already has answers here : Assignment of variables with space after the (=) sign? (3 answers) Closed 4 months ago . #!/bin/bash declare -r NUM1=5 NUM2 =4 # Line 4 num3=$((NUM1 + NUM2)) num4=$((NUM1 - NUM2)) num5=$((NUM1 * NUM2)) num6=$((NUM1 / NUM2)) # Line 9 echo \"$num3\" echo $((5**2)) echo $((5%4)) I am using this bash script, and when I was running the script, I got the error ./bash_help ./bash_help: line 4: NUM2: command not found ./bash_help: line 9: NUM1 / NUM2: division

JavaScript code trick: What&#39;s the value of foo.x

北城以北 提交于 2019-11-26 02:55:11
问题 I found this problem in a GitHub front-end interview questions collection: var foo = {n: 1}; var bar = foo; foo.x = foo = {n: 2}; Question: What is the value of foo.x? The answer is undefined . I\'ve done some research and what I understand this problem is (correct me if I\'m wrong): var foo = {n: 1}; declares an object foo which has property n equal to 1. var bar = foo; declares an object bar which refers to the same object as foo . foo.x = foo = {n: 2}; which I believe is equal to foo.x =

How to assign from a function which returns more than one value?

落爺英雄遲暮 提交于 2019-11-26 02:39:21
Still trying to get into the R logic... what is the "best" way to unpack (on LHS) the results from a function returning multiple values? I can't do this apparently: R> functionReturningTwoValues <- function() { return(c(1, 2)) } R> functionReturningTwoValues() [1] 1 2 R> a, b <- functionReturningTwoValues() Error: unexpected ',' in "a," R> c(a, b) <- functionReturningTwoValues() Error in c(a, b) <- functionReturningTwoValues() : object 'a' not found must I really do the following? R> r <- functionReturningTwoValues() R> a <- r[1]; b <- r[2] or would the R programmer write something more like

Why can not I add two bytes and get an int and I can add two final bytes get a byte?

谁说胖子不能爱 提交于 2019-11-26 02:37:39
问题 public class Java{ public static void main(String[] args){ final byte x = 1; final byte y = 2; byte z = x + y;//ok System.out.println(z); byte a = 1; byte b = 2; byte c = a + b; //Compiler error System.out.println(c); } } If the result of an expression involving anything int-sized or smaller is always an int even if the sum of two bytes fit in a byte. Why does it happen when we add two final bytes that fit in a byte? There is no compiler error. 回答1: From the JLS 5.2 Assignment Conversion In

Operator precedence with Javascript Ternary operator

六眼飞鱼酱① 提交于 2019-11-26 02:32:14
问题 I cant seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator. h.className += h.className ? \' error\' : \'error\' The way i think this code works is as following: h.className = h.className + h.className ? \' error\' : \'error\' But that isn\'t correct because that gives a error in my console. So my question is how should i interpet this code correctly? 回答1: h.className = h.className + (h.className ? ' error' : 'error') You want the operator to

Copy constructor and = operator overload in C++: is a common function possible?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 02:27:27
问题 Since a copy constructor MyClass(const MyClass&); and an = operator overload MyClass& operator = (const MyClass&); have pretty much the same code, the same parameter, and only differ on the return, is it possible to have a common function for them both to use? 回答1: Yes. There are two common options. One - which is generally discouraged - is to call the operator= from the copy constructor explicitly: MyClass(const MyClass& other) { operator=(other); } However, providing a good operator= is a

JavaScript OR (||) variable assignment explanation

試著忘記壹切 提交于 2019-11-26 02:25:17
问题 Given this snippet of JavaScript... var a; var b = null; var c = undefined; var d = 4; var e = \'five\'; var f = a || b || c || d || e; alert(f); // 4 Can someone please explain to me what this technique is called (my best guess is in the title of this question!)? And how/why it works exactly? My understanding is that variable f will be assigned the nearest value (from left to right) of the first variable that has a value that isn\'t either null or undefined, but I\'ve not managed to find