mutable

Correct Style for Python functions that mutate the argument

旧城冷巷雨未停 提交于 2019-11-27 08:42:19
I would like to write a Python function that mutates one of the arguments (which is a list, ie, mutable). Something like this: def change(array): array.append(4) change(array) I'm more familiar with passing by value than Python's setup (whatever you decide to call it). So I would usually write such a function like this: def change(array): array.append(4) return array array = change(array) Here's my confusion. Since I can just mutate the argument, the second method would seem redundant. But the first one feels wrong. Also, my particular function will have several parameters, only one of which

Are Python Lists mutable?

情到浓时终转凉″ 提交于 2019-11-27 08:06:14
问题 When I type following code, x=[1,2,4] print(x) print("x",id(x)) x=[2,5,3] print(x) print("x",id(x)) it gives the output as [1, 2, 4] x 47606160 [2, 5, 3] x 47578768 If lists are mutable then why it give 2 memory address when changing the list x? 回答1: You did not mutate (change) the list object referenced by x with this line: x=[2,5,3] Instead, that line creates a new list object and then reassigns the variable x to it. So, x now references the new object and id(x) gives a different number

When changing the value of a variable in C, is a new primitive created or is the current primitive mutated?

依然范特西╮ 提交于 2019-11-27 07:56:50
问题 I know that 'mutable' and 'immutable' are terms that should be used to describe the ability for objects to change value in object oriented languages such as Java and Objective C. However, I would like to bring it up because it relates to my question regarding primitives data. I know that when I change the value of a variable holding immutable object, I am in fact creating a new object. I wonder, however, if primitives data in C behave similarly to immutable objects. By that I mean, when I

Immutable/Mutable Collections in Swift

天涯浪子 提交于 2019-11-27 06:21:54
I was referring to Apple's Swift programming guide for understanding creation of Mutable/ immutable objects(Array, Dictionary, Sets, Data) in Swift language. But I could't understand how to create a immutable collections in Swift. I would like to see the equivalents in Swift for the following in Objective-C Immutable Array NSArray *imArray = [[NSArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; Mutable Array NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; [mArray addObject:@"Fourth"]; Immutable Dictionary NSDictionary *imDictionary

Should mutexes be mutable?

只谈情不闲聊 提交于 2019-11-27 05:48:17
问题 Not sure if this is a style question, or something that has a hard rule... If I want to keep the public method interface as const as possible, but make the object thread safe, should I use mutable mutexes? In general, is this good style, or should a non-const method interface be preferred? Please justify your view. 回答1: [ Answer edited ] Basically using const methods with mutable mutexes is a good idea (don't return references by the way, make sure to return by value), at least to indicate

Which JavaScript Array functions are mutating?

 ̄綄美尐妖づ 提交于 2019-11-27 05:18:37
问题 I am writing an Array-derived class in JavaScript and need to know which functions to overload so that I can be aware of changes made to the array. I know Array.push() and Array.splice() are mutating. Is there a definitive list of any others? 回答1: You can find the list on MDN as Mutator methods (along with Accessor and Iteration methods): copyWithin fill pop push reverse shift sort splice unshift 回答2: You can also use .concat() , before using your mutation method, to ensure you are not

Cannot borrow `*x` as mutable because it is also borrowed as immutable

倖福魔咒の 提交于 2019-11-27 04:53:35
问题 I'm making a Combinatory Optimization project to learn Rust and I've got a problem I cannot resolve myself... I've got 2 functions : pub fn get_pareto_front_offline<'a>(scheduling_jobs: &'a Vec<Vec<u32>>, costs_vector: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> { // ... } and pub fn pareto_approach_offline<'a>(list_of_jobs: &'a mut Vec<Vec<u32>>, neighborhood: &'a mut Vec<Vec<u32>>, costs: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> { let pareto_front = get

Why the “mutable default argument fix” syntax is so ugly, asks python newbie

て烟熏妆下的殇ゞ 提交于 2019-11-27 04:36:11
Now following my series of "python newbie questions" and based on another question . Prerogative Go to http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables and scroll down to "Default Parameter Values". There you can find the following: def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list def good_append(new_item, a_list=None): if a_list is None: a_list = [] a_list.append(new_item) return a_list There's even an "Important warning" on python.org with this very same example, tho not really saying it's "better". One way to put

Const method that modifies *this without const_cast

╄→尐↘猪︶ㄣ 提交于 2019-11-27 04:26:03
问题 The following pattern has arisen in a program I'm writing. I hope it's not too contrived, but it manages to mutate a Foo object in the const method Foo::Questionable() const , without use of any const_cast or similar. Basically, Foo stores a reference to FooOwner and vice versa, and in Questionable() , Foo manages to modify itself in a const method by calling mutate_foo() on its owner. Questions follow the code. #include "stdafx.h" #include <iostream> using namespace std; class FooOwner;

Using volatile keyword with mutable object

时光总嘲笑我的痴心妄想 提交于 2019-11-27 04:23:39
问题 In Java, I understand that volatile keyword provides visibility to variables. The question is, if a variable is a reference to a mutable object, does volatile also provide visibility to the members inside that object? In the example below, does it work correctly if multiple threads are accessing volatile Mutable m and changing the value ? example class Mutable { private int value; public int get() { return a; } public int set(int value) { this.value = value; } } class Test { public volatile