variable-assignment

Is a += b more efficient than a = a + b in C?

自古美人都是妖i 提交于 2019-12-17 19:16:10
问题 I know in some languages the following: a += b is more efficient than: a = a + b because it removes the need for creating a temporary variable. Is this the case in C? Is it more efficient to use += (and, therefore also -= *= etc) 回答1: So here's a definitive answer... $ cat junk1.c #include <stdio.h> int main() { long a, s = 0; for (a = 0; a < 1000000000; a++) { s = s + a * a; } printf("Final sum: %ld\n", s); } michael@isolde:~/junk$ cat junk2.c #include <stdio.h> int main() { long a, s = 0;

Python Multiple Assignment Statements In One Line

三世轮回 提交于 2019-12-17 18:01:16
问题 (Don't worry, this isn't another question about unpacking tuples.) In python, a statement like foo = bar = baz = 5 assigns the variables foo, bar, and baz to 5. It assigns these variables from left to right, as can be proved by nastier examples like >>> foo[0] = foo = [0] Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'foo' is not defined >>> foo = foo[0] = [0] >>> foo [[...]] >>> foo[0] [[...]] >>> foo is foo[0] True But the python language reference

Is it possible only to declare a variable without assigning any value in Python?

ぐ巨炮叔叔 提交于 2019-12-17 17:20:14
问题 Is it possible to declare a variable in Python, like so?: var so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why? EDIT: I want to do this for cases like this: value for index in sequence: if value == None and conditionMet: value = index break Duplicate Uninitialised value in python (by same author) Are there any declaration keywords in Python? (by the same author) Related Python: variable scope and function

Test if an argument of a function is set or not in R

北城以北 提交于 2019-12-17 15:34:48
问题 I have a function f that takes two parameters ( p1 and p2 ): If for the parameter p2 no value was passed to the function, the value of p1 ^2 should be used instead. But how can I find out within the function, if a value is given or not. The problem is that the variable p2 is not initialized if there was no value. Thus I can't test for p2 being NULL . f <- function(p1, p2) { if(is.null(p2)) { p2=p1^2 } p1-p2 } Is it somehow possible to check if a value for p2 was passed to the function or not?

myView.frame.origin.x = value; does not work - But why?

馋奶兔 提交于 2019-12-17 10:27:09
问题 I know that I can't use this: myView.frame.origin.x = 25.0; and that I have to use this instead: CGRect myFrame = myView.frame; myFrame.origin.x = 25.0; myView.frame = myFrame; And I'm doing it all the time, but I don't know why I must do it that way. I would like to fill that gap in my understanding. Can someone explain ? Nowadays Xcode gives you "Expression not assignable". Some time ago you got a compile error "Lvalue required as left operand of assignment". 回答1: There are two distinct dot

What's the difference between `=` and `<-` in R? [duplicate]

蓝咒 提交于 2019-12-17 10:12:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Assignment operators in R: '=' and '<-' I'm using R 2.8.1 and it is possible to use both = and <- as variable assignment operators. What's the difference between them? Which one should I use? 回答1: From here: The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression

Assigning char array a value in C

你。 提交于 2019-12-17 09:55:40
问题 What is the difference between: char fast_car[15]="Bugatti"; and char fast_car[15]; fast_car="Bugatti"; Because the second one results with compile error: error: incompatible types when assigning to type ‘char[15]’ from type ‘char *’ While the first one works fine. Putting a string in array in different place than array initialisation would be helpful. 回答1: The first is an initialization while the second is an assignment . Since arrays aren't modifiable values in C you can't assign new values

assigning class variable as default value to class method argument

点点圈 提交于 2019-12-17 09:39:20
问题 I would like to build a method inside a class with default values arguments taken from this class. In general I do filtering on some data. Inside my class I have a method where normally I pass vector of data. Sometimes I don't have the vector and I take simulated data. Every time I do not pass a particular vector I would like to take simulated data by default. I thought it should be an easy construction where inside my method definition I say a=self.vector . But for some reason I have an

Is a variable the name, the value, or the memory location?

怎甘沉沦 提交于 2019-12-17 07:40:14
问题 I've been learning Python for a few months, and also know very little C, and I was wondering if anyone could clear this doubt up for me: Is a variable the name, the value, or the memory location? For example: x = 5 Is the variable x, the value of x, or the location of x in memory? I'm looking for a clear explanation of what a variable is. I've already looked at Wikipedia's page on variables and this question, but neither were too clear to me. If this is a duplicate question, a link to the

Double Pipe Symbols in Ruby Variable Assignment? [duplicate]

懵懂的女人 提交于 2019-12-17 07:17:31
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What does ||= mean in Ruby? Forgive me if this is a newby question but im reading a book on rails where the writer used this expression in a helper method: @current_user ||= User.find_by_id(session[:user_id]) Is this use of double pipes still a Boolean OR statement? If so how does it work? 回答1: It's a conditional assignment. From here: x = find_something() #=>nil x ||= "default" #=>"default" : value of x will be