mutable

Good uses for mutable function argument default values?

谁说胖子不能爱 提交于 2019-11-27 18:17:29
It is a common mistake in Python to set a mutable object as the default value of an argument in a function. Here's an example taken from this excellent write-up by David Goodger : >>> def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list >>> print bad_append('one') ['one'] >>> print bad_append('two') ['one', 'two'] The explanation why this happens is here . And now for my question: Is there a good use-case for this syntax? I mean, if everybody who encounters it makes the same mistake, debugs it, understands the issue and from thereon tries to avoid it, what use is there

mutable fields for objects in a Java Set

人走茶凉 提交于 2019-11-27 17:52:13
问题 Am I correct in assuming that if you have an object that is contained inside a Java Set<> (or as a key in a Map<> for that matter), any fields that are used to determine identity or relation (via hashCode() , equals() , compareTo() etc.) cannot be changed without causing unspecified behavior for operations on the collection? (edit: as alluded to in this other question) (In other words, these fields should either be immutable, or you should require the object to be removed from the collection,

Is making in-place operations return the object a bad idea?

痴心易碎 提交于 2019-11-27 17:14:42
问题 I'm talking mostly about Python here, but I suppose this probably holds for most languages. If I have a mutable object, is it a bad idea to make an in-place operation also return the object? It seems like most examples just modify the object and return None . For example, list.sort . 回答1: Yes, it is a bad idea. The reason is that if in-place and non-in-place operations have apparently identical output, then programmers will frequently mix up in-place operations and non-in-place operations (

Existence of mutable named tuple in Python?

对着背影说爱祢 提交于 2019-11-27 17:00:54
Can anyone amend namedtuple or provide an alternative class so that it works for mutable objects? Primarily for readability, I would like something similar to namedtuple that does this: from Camelot import namedgroup Point = namedgroup('Point', ['x', 'y']) p = Point(0, 0) p.x = 10 >>> p Point(x=10, y=0) >>> p.x *= 10 Point(x=100, y=0) It must be possible to pickle the resulting object. And per the characteristics of named tuple, the ordering of the output when represented must match the order of the parameter list when constructing the object. There is a mutable alternative to collections

volatile vs. mutable in C++

空扰寡人 提交于 2019-11-27 16:47:11
I have a question about the difference between volatile and mutable. I noticed that both of the two means that it could be changed. What else? Are they the same thing? What's the difference? Where are they applicable? Why the two ideas are proposed? How to use them in different way? Thanks a lot. A mutable field can be changed even in an object accessed through a const pointer or reference, or in a const object, so the compiler knows not to stash it in R/O memory. A volatile location is one that can be changed by code the compiler doesn't know about (e.g. some kernel-level driver), so the

Are strings mutable in Ruby?

送分小仙女□ 提交于 2019-11-27 14:17:20
Are Strings mutable in Ruby? According to the documentation doing str = "hello" str = str + " world" creates a new string object with the value "hello world" but when we do str = "hello" str << " world" It does not mention that it creates a new object, so does it mutate the str object, which will now have the value "hello world" ? Dogbert Yes, << mutates the same object, and + creates a new one. Demonstration: irb(main):011:0> str = "hello" => "hello" irb(main):012:0> str.object_id => 22269036 irb(main):013:0> str << " world" => "hello world" irb(main):014:0> str.object_id => 22269036 irb(main

How I can mutate a struct's field from a method?

不想你离开。 提交于 2019-11-27 13:24:24
问题 I want to do this: struct Point { x: i32, y: i32, } impl Point { fn up(&self) { self.y += 1; } } fn main() { let p = Point { x: 0, y: 0 }; p.up(); } But this code throws a compiler error: error[E0594]: cannot assign to field `self.y` of immutable binding --> src/main.rs:8:9 | 7 | fn up(&self) { | ----- use `&mut self` here to make mutable 8 | self.y += 1; | ^^^^^^^^^^^ cannot mutably borrow field of immutable binding 回答1: You need to use &mut self instead of &self and make the p variable

Why does capturing a mutable struct variable inside a closure within a using statement change its local behavior?

≡放荡痞女 提交于 2019-11-27 11:39:46
问题 Update : Well, now I've gone and done it: I filed a bug report with Microsoft about this, as I seriously doubt that it is correct behavior. That said, I'm still not 100% sure what to believe regarding this question; so I can see that what is "correct" is open to some level of interpretation. My feeling is that either Microsoft will accept that this is a bug, or else respond that the modification of a mutable value type variable within a using statement constitutes undefined behavior. Also,

Is the use of del bad?

♀尐吖头ヾ 提交于 2019-11-27 11:28:14
问题 I commonly use del in my code to delete objects: >>> array = [4, 6, 7, 'hello', 8] >>> del(array[array.index('hello')]) >>> array [4, 6, 7, 8] >>> But I have heard many people say that the use of del is unpythonic. Is using del bad practice? >>> array = [4, 6, 7, 'hello', 8] >>> array[array.index('hello'):array.index('hello')+1] = '' >>> array [4, 6, 7, 8] >>> If not, why are there many ways to accomplish the same thing in python? Is one better than the others? Option 1: using del >>> arr =

Why are integers immutable in Python? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 08:54:13
This question already has an answer here: Immutable vs Mutable types 16 answers I understand the differences between mutable and immutable objects in Python. I have read many posts discussing the differences. However, I have not read anything regarding WHY integers are immutable objects. Does there exist a reason for this? Or is the answer "that's just how it is"? Edit: I am getting prompted to 'differentiate' this question from other questions as it seems to be a previously asked question. However, I believe what I'm asking is more of a philosophical Python question rather than a technical