variable-assignment

Why does foo = filter(…) return a <filter object>, not a list? [duplicate]

谁说我不能喝 提交于 2019-11-26 20:23:02
问题 This question already has an answer here: Calling filter returns <filter object at … > [duplicate] 2 answers Working in Python IDLE 3.5.0 shell. From my understanding of the builtin "filter" function it returns either a list, tuple, or string, depending on what you pass into it. So, why does the first assignment below work, but not the second (the '>>>'s are just the interactive Python prompts) >>> def greetings(): return "hello" >>> hesaid = greetings() >>> print(hesaid) hello >>> >>>

Multiple variable assignment in Swift

梦想与她 提交于 2019-11-26 20:06:44
问题 How do I assign multiple variables in one line using Swift? var blah = 0 var blah2 = 2 blah = blah2 = 3 // Doesn't work??? 回答1: You don't. This is a language feature to prevent the standard unwanted side-effect of assignment returning a value, as described in the Swift book: Unlike the assignment operator in C and Objective-C, the assignment operator in Swift does not itself return a value. The following statement is not valid: if x = y { // this is not valid, because x = y does not return a

How does javascript logical assignment work?

我的梦境 提交于 2019-11-26 20:06:19
问题 In javascript, if we have some code such as var a = "one"; var b = q || a; alert (b); The logical OR operator will assign a's value to b, and the alert will be "one." Is this limited to assignments only or can we use it everywhere? It seems an empty string is treated the same as undefined. Is this right? How does this work with AND variables? What about combinations of them? What is a good example of when to use these idioms, or when not to? 回答1: For your q || a to evaluate to a , q should be

Return value of assignment operator in concurrent code

只谈情不闲聊 提交于 2019-11-26 19:57:15
问题 Given the following class: class Foo { public volatile int number; public int method1() { int ret = number = 1; return ret; } public int method2() { int ret = number = 2; return ret; } } and given multiple threads calling method1() and method2() concurrently on the same Foo instance, can a call to method1() ever return anything other than 1? 回答1: The JLS 15.26 specifies: There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=

Is it possible to have tuple assignment to variables in Scala? [duplicate]

扶醉桌前 提交于 2019-11-26 19:55:46
问题 Possible Duplicate: Tuple parameter declaration and assignment oddity In Scala, one can do multiple-variable assignment to tuples via: val (a, b) = (1, 2) But a similar syntax for assignment to variables doesn't appear to work. For example I'd like to do this: var (c, d) = (3, 4) (c, d) = (5, 6) I'd like to reuse c and d in multiple-variable assignment. Is this possible? 回答1: This isn't simply "multiple variable assignment", it's fully-featured pattern matching! So the following are all valid

How do I copy the contents of one ArrayList into another?

…衆ロ難τιáo~ 提交于 2019-11-26 19:35:32
I have some data structures, and I would like to use one as a temporary, and another as not temporary. ArrayList<Object> myObject = new ArrayList<Object>(); ArrayList<Object> myTempObject = new ArrayList<Object>(); //fill myTempObject here .... //make myObject contain the same values as myTempObject myObject = myTempObject; //free up memory by clearing myTempObject myTempObject.clear(); now the problem with this of course is that myObject is really just pointing to myTempObject , and so once myTempObject is cleared, so is myObject . How do I retain the values from myTempObject in myObject

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

南笙酒味 提交于 2019-11-26 18:42:33
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? Martijn Pieters 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 iterable on the right-hand side, and a list or tuple of names on the left. When you do: [] = "" you assigned

Constant declaration with block

女生的网名这么多〃 提交于 2019-11-26 18:32:30
问题 Recently I was looking into Firefox Add-on Builder SDK sources, and stumbled on such constants declaration: const { getCodeForKey, toJSON } = require("../../keyboard/utils"); I could find information about CommonJS Modules, but left part of this assignment slightly confuses me, since it must be language specific, and I couldn't google anything on that. Can someone point me to some specification/draft that explains what's going on here? 回答1: This is a destructuring assignment, something that

Why is `i = ++i + 1` unspecified behavior?

﹥>﹥吖頭↗ 提交于 2019-11-26 18:31:49
Consider the following C++ Standard ISO/IEC 14882:2003(E) citation (section 5, paragraph 4): Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. 53) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering

Destructuring assignment in JavaScript

╄→尐↘猪︶ㄣ 提交于 2019-11-26 17:50:58
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(); var i = 0; $.each(map, function(e, _) { o[e] = array[i++]; }); return o; } Which works like: var array =