variable-assignment

Why can I use the same name for iterator and sequence in a Python for loop?

谁说胖子不能爱 提交于 2019-11-27 00:32:49
问题 This is more of a conceptual question. I recently saw a piece of code in Python (it worked in 2.7, and it might also have been run in 2.5 as well) in which a for loop used the same name for both the list that was being iterated over and the item in the list, which strikes me as both bad practice and something that should not work at all. For example: x = [1,2,3,4,5] for x in x: print x print x Yields: 1 2 3 4 5 5 Now, it makes sense to me that the last value printed would be the last value

Javascript - Assigning multiple variables to object properties using curly braces in variable declaration

有些话、适合烂在心里 提交于 2019-11-26 23:21:13
问题 While looking at some Javascript code for Mozilla's (Firefox) Add-on SDK, I saw kind of variable declaration I hadn't seen before: var { foo, bar } = someFunction("whatever"); // just an example See those curly braces around the variable name? Turns out, this is a way of assigning the values of properties of an object to multiple variables all at once. It seems similar to destructuring assignment or PHP's list, except with object properties instead of arrays. I actually found this out through

Multiple Unpacking Assignment in Python when you don't know the sequence length

断了今生、忘了曾经 提交于 2019-11-26 23:14:38
问题 The textbook examples of multiple unpacking assignment are something like: import numpy as NP M = NP.arange(5) a, b, c, d, e = M # so of course, a = 0, b = 1, etc. M = NP.arange(20).reshape(5, 4) # numpy 5x4 array a, b, c, d, e = M # here, a = M[0,:], b = M[1,:], etc. (ie, a single row of M is assigned each to a through e) (My question is not numpy specific. Indeed, I would prefer a pure Python solution.) For the piece of code I'm looking at now, I see two complications on that

Java Object Assignment

核能气质少年 提交于 2019-11-26 22:46:32
问题 I am new to Java and I have some questions in mind regarding object assignment. For instance, Test t1 = new Test(); Test t2 = t1; t1.i=1; Assuming variable i is defined inside Test class, am I right to assume both t1 and t2 point to the same object where the modification t1.i=1 affects both t1 and t2 ? Actually I tested it out and seems like I was right. However when I try the same thing on String , the modification happens only on one side where the other side is unaffected. What is the

Local variable referenced before assignment in Python?

核能气质少年 提交于 2019-11-26 22:38:20
I am using the PyQt library to take a screenshot of a webpage, then reading through a CSV file of different URLs. I am keeping a variable feed that incremements everytime a URL is processed and therefore should increment to the number of URLs. Here's code: webpage = QWebPage() fo = open("C:/Users/Romi/Desktop/result1.txt", "w") feed = 0 def onLoadFinished(result): #fo.write( column1[feed])#, column2[feed], urls[feed]) #feed = 0 if not result: print "Request failed" fo.write(column1[feed]) fo.write(',') fo.write(column2[feed]) fo.write(',') #fo.write(urls[feed]) fo.write(',') fo.write('404

In JavaScript, is chained assignment okay?

孤人 提交于 2019-11-26 22:17:01
问题 Am not new to JS or its syntax, but sometimes, the semantics of the language has me stumped at times. At work today, a colleague mentioned this: var a = b = []; is not the same as var a = [], b = []; or var a = []; var b = []; since the first version actually assigns the reference to an empty array to a and b. I couldn't quite accept this as true, but I'm not sure. What do you all think? 回答1: Yes, they're not the same. var a = b = [] is equivalent to var a; b = []; a = b; Not only do both a

Can a JavaScript object property refer to another property of the same object? [duplicate]

北慕城南 提交于 2019-11-26 22:09:31
This question already has an answer here: Self-references in object literals / initializers 23 answers I recently tried to create an object like this: var carousel = { $slider: $('#carousel1 .slider'), panes: carousel.$slider.children().length }; My intentions were to improve jQuery's selector performance by caching the results of $('#carousel1 .slider') in an object property, and to keep the code concise and relatively DRY. However, this didn't work. When the code executed, it threw an exception when trying to parse the value of panes , complaining that carousel was undefined. This makes

Auto-align “=” in assignments

心不动则不痛 提交于 2019-11-26 20:58:36
问题 How do one convert a block of variable assignments, e.g.: private final String s1 = "10011"; private final String s2 = "01100"; private final String ones = "11111"; private final String zeros = "00000"; To a straightened format where all = signs are aligned to one space after the longest left-hand side, e.g.: private final String s1 = "10011"; private final String s2 = "01100"; private final String ones = "11111"; private final String zeros = "00000"; 回答1: Preferences → Java → Code Style →

Difference between a += 10 and a = a + 10 in java? [duplicate]

只谈情不闲聊 提交于 2019-11-26 20:45:49
This question already has an answer here: Why don't Java's +=, -=, *=, /= compound assignment operators require casting? 11 answers Are a += 10 and a = a + 10 both the same, or is there some difference between them? I got this question while studying assignments in Java. As you've now mentioned casting... there is a difference in this case: byte a = 5; a += 10; // Valid a = a + 10; // Invalid, as the expression "a + 10" is of type int From the Java Language Specification section 15.26.2 : A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)) , where T

Simultaneous assignment semantics in Python

谁说胖子不能爱 提交于 2019-11-26 20:41:15
问题 Consider the following Python 3 code: a = [-1,-1,-1] i = 0 And now consider the following two versions of a simultaneous assignment over both a and i: Assignment version 1: a[i],i = i,i+1 Assignment version 2: i,a[i] = i+1,i I would expect these two versions of simultaneous assignments to be semantically equivalent. However, if you check the values of a and i after each one of the simultaneous assignments, you get different states: Output for print(a,i) after assignment version 1: [0, -1, -1]