variable-assignment

What's the rationale for preventing assignment to arrays?

早过忘川 提交于 2019-11-29 13:07:41
I've tried to google this and have read: Why can't arrays of same type and size be assigned? Assigning arrays Assign to array in struct in c But they all state the obvious: you can't assign to arrays because the standard says so. That's great and all, but I want to know why the standard doesn't include support for assigning to arrays. The standard committee discusses things in detail, and I'd be surprised if they never discussed making arrays assignable. Assuming they've discussed it, they must have some rationale for not letting arrays be assigned to. I mean, we can put an array in a struct

Types for which “is” keyword may be equivalent to equality operator in Python

£可爱£侵袭症+ 提交于 2019-11-29 11:47:32
For some types in Python, the is operator seems to be equivalent to the == operator. For example: >>> 1 is 1 True >>> "a spoon" is "a spoon" True >>> (1 == 1) is (2 == 2) True However, this is not always the case: >>> [] == [] True >>> [] is [] False This makes sense for mutable types such as lists. However, immutable types such as tuples seem to display the same behavior: >>> (1, 2) == (1, 2) True >>> (1, 2) is (1, 2) False This raises several questions: Is the == / is equivalence related to immutability? Are the behaviors above specified, or an implementation detail? Most importantly (and

numpy function to set elements of array to a value given a list of indices

匆匆过客 提交于 2019-11-29 11:42:30
问题 I'm looking for a numpy function that will do the equivalent of: indices = set([1, 4, 5, 6, 7]) zero = numpy.zeros(10) for i in indices: zero[i] = 42 回答1: You can just give it a list of indices: indices = [1, 4, 5, 6, 7] zero = numpy.zeros(10) zero[indices] = 42 回答2: If you have an ndarry: >>> x = np.zeros((3, 3, 3)) >>> y = [0, 9, 18] >>> x array([[[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]], [[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]], [[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]]) >>>

Direct Initialization vs Copy Initialization for Primitives

徘徊边缘 提交于 2019-11-29 11:40:14
When initializing primitive types like int or pointers one can use either copy-initialization or direct-initialization . int a = 10; int b(10); Although the latter way is preffered for objects with constructors, I don't see people using it for primitives. I understand that it is kind of "more natural" (especially for numbers) to use the '=' operator but is there anybody writing things like in real-life code: for (int i(0); i < 5; ++i) { cout << i << endl; } Thanks. EDIT: The question asks about coding styles and best practices rather than technical implementation. Some people do this to be

Any more concise way to set default values?

柔情痞子 提交于 2019-11-29 11:00:00
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise. Is there any better or more concise way than following code to set default value of variables? $v = isset($v) ? $v : "default value"; Here is a shorter syntax: isset($v) || $v="default value"; TL;DR - No, that expression can't be made any shorter. What you want is for the shortened ternary expression to perform an implicit isset() . This has been discussed on the mailing list and an ifsetor RFC has been created that covers

Assigning an integer literal to a double variable in Java

百般思念 提交于 2019-11-29 10:56:13
If I do the following double d = 0; since 0 is an integer literal, which uses 32 bits, and d is a double variable that uses 64 bits, will the remaining 32 bits be filled with random garbage, or does Java promote the literal correctly? Java promotes it correctly, otherwise there'd be a rather large body of code that was problematic :-) Section 5.1.2 of the Java language spec details this: The following 19 specific conversions on primitive types are called the widening primitive conversions: byte to short, int, long, float, or double short to int, long, float, or double char to int, long, float,

C++: Set bool value only if not set

▼魔方 西西 提交于 2019-11-29 10:16:17
I have code in my C++ application that generally does this: bool myFlag = false; while (/*some finite condition unrelated to myFlag*/) { if (...) { // statements, unrelated to myFlag } else { // set myFlag to true, perhaps only if it was false before? } } if (myFlag) { // Do something... } The question I have pertains to the else statement of my code. Basically, my loop may set the value of myFlag from false to true, based on a certain condition not being met. Never will the flag be unset from true to false. I would like to know what statement makes more sense performance-wise and perhaps if

Is pointer assignment atomic in C++?

好久不见. 提交于 2019-11-29 09:25:41
I've actually heard claims both ways. I suspect they are not, but I wanted to get the topic settled. C++03 does not know about the existance of threads, therefore the concept of atomicity doesn't make much sense for C++03, meaning that it doesn't say anything about that. C++11 does know about threads, but once again doesn't say anything about the atomicity of assigning pointers. However C++11 does contain std::atomic<T*> , which is guaranteed to be atomic. Note that even if writing to a raw pointer is atomic on your platform the compiler is still free to move that assingment around, so that

Fixing the 'Use of unassigned local variable' with a null assignment. Why?

大兔子大兔子 提交于 2019-11-29 09:03:54
With a piece of code like this, the compiler complains on c.MyProperty : MyClass c; try { throw new Exception(); } catch (Exception) { } c.MyProperty = 2; // "Use of unassigned local variable 'c'". Yet it doesn't complain if you assign a null to c in initialization: MyClass c = null; try { throw new Exception(); } catch (Exception) { } c.MyProperty = 2; // no complains this time. So, why does this work? If c wasn't assigned a null and the compiler hypothetically allowed it, wouldn't the same exception be thrown at c.MyProperty , Object reference not set to an instance of an object ? When you

Assigning an IronPython method to a C# delegate

北战南征 提交于 2019-11-29 07:42:13
I have a C# class that looks a little like: public class MyClass { private Func<IDataCource, object> processMethod = (ds) => { //default method for the class } public Func<IDataCource, object> ProcessMethod { get{ return processMethod; } set{ processMethod = value; } } /* Other details elided */ } And I have an IronPython script that gets run in the application that looks like from MyApp import myObj #instance of MyClass def OtherMethod(ds): if ds.Data.Length > 0 : quot = sum(ds.Data.Real)/sum(ds.Data.Imag) return quot return 0.0 myObj.ProcessMethod = OtherMethod But when ProcessMethod gets