mutable

Python object initialization bug. Or am I misunderstanding how objects work?

末鹿安然 提交于 2019-12-02 07:48:12
问题 1 import sys 2 3 class dummy(object): 4 def __init__(self, val): 5 self.val = val 6 7 class myobj(object): 8 def __init__(self, resources): 9 self._resources = resources 10 11 class ext(myobj): 12 def __init__(self, resources=[]): 13 #myobj.__init__(self, resources) 14 self._resources = resources 15 16 one = ext() 17 one._resources.append(1) 18 two = ext() 19 20 print one._resources 21 print two._resources 22 23 sys.exit(0) This will print the reference to the object assigned to one.

How to implement IEquatable<T> when mutable fields are part of the equality - Problem with GetHashCode

假装没事ソ 提交于 2019-12-02 07:27:05
I am using Entity Framework in my application. I implemented with the partial class of an entity the IEquatable<T> interface: Partial Class Address : Implements IEquatable(Of Address) 'Other part generated Public Overloads Function Equals(ByVal other As Address) As Boolean _ Implements System.IEquatable(Of Address).Equals If ReferenceEquals(Me, other) Then Return True Return AddressId = other.AddressId End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If obj Is Nothing Then Return MyBase.Equals(obj) If TypeOf obj Is Address Then Return Equals(DirectCast(obj, Address

Push doesn't modify the list being a function argument

独自空忆成欢 提交于 2019-12-02 04:46:18
问题 I'm new to common lisp, so hope someone would clarify this to me: say we have a list and want to add an item with push to modify it: CL-USER> (defparameter xx '(1 2 3)) XX CL-USER> xx (1 2 3) CL-USER> (push 100 xx) (100 1 2 3) CL-USER> xx (100 1 2 3) as expected. But when i try to do the same with the function, it doesn't modify a list: CL-USER> (defun push-200 (my-list) (push 200 my-list)) PUSH-200 CL-USER> (push-200 xx) (200 100 1 2 3) CL-USER> xx (100 1 2 3) so i tried to compare argument

Python object initialization bug. Or am I misunderstanding how objects work?

风流意气都作罢 提交于 2019-12-02 03:58:05
1 import sys 2 3 class dummy(object): 4 def __init__(self, val): 5 self.val = val 6 7 class myobj(object): 8 def __init__(self, resources): 9 self._resources = resources 10 11 class ext(myobj): 12 def __init__(self, resources=[]): 13 #myobj.__init__(self, resources) 14 self._resources = resources 15 16 one = ext() 17 one._resources.append(1) 18 two = ext() 19 20 print one._resources 21 print two._resources 22 23 sys.exit(0) This will print the reference to the object assigned to one._resources for both one and two objects. I would think that two would be an empty array as it is clearly setting

Iterating through a recursive structure using mutable references and returning the last valid reference

限于喜欢 提交于 2019-12-02 02:01:27
问题 I'm trying to recurse down a structure of nodes, modifying them, and then returning the last Node that I get to. I solved the problems with mutable references in the loop using an example in the non-lexical lifetimes RFC. If I try to return the mutable reference to the last Node , I get a use of moved value error: #[derive(Debug)] struct Node { children: Vec<Node>, } impl Node { fn new(children: Vec<Self>) -> Self { Self { children } } fn get_last(&mut self) -> Option<&mut Node> { self

Simple as possible example of returning a mutable reference from your own iterator

こ雲淡風輕ζ 提交于 2019-12-02 02:01:08
问题 This question is related, however it moreso covers the reason why the compiler cannot infer a safe lifetime when returning a mutable reference from Iterator::next , which I think I understand. My question is: What are the specific steps you can take when designing your own iterator so that it can produce mutable references? Ultimately, I'm hoping for a concise-as-possible, step-by-step, commented example of both an Iterator and its next implementation that I (and anyone) can go to as a clear

Push doesn't modify the list being a function argument

无人久伴 提交于 2019-12-02 01:21:53
I'm new to common lisp, so hope someone would clarify this to me: say we have a list and want to add an item with push to modify it: CL-USER> (defparameter xx '(1 2 3)) XX CL-USER> xx (1 2 3) CL-USER> (push 100 xx) (100 1 2 3) CL-USER> xx (100 1 2 3) as expected. But when i try to do the same with the function, it doesn't modify a list: CL-USER> (defun push-200 (my-list) (push 200 my-list)) PUSH-200 CL-USER> (push-200 xx) (200 100 1 2 3) CL-USER> xx (100 1 2 3) so i tried to compare argument and my list like this: CL-USER> (defun push-200 (my-list) (format t "~a" (eq my-list xx)) (push 200 my

Iterating through a recursive structure using mutable references and returning the last valid reference

﹥>﹥吖頭↗ 提交于 2019-12-01 23:02:58
I'm trying to recurse down a structure of nodes, modifying them, and then returning the last Node that I get to. I solved the problems with mutable references in the loop using an example in the non-lexical lifetimes RFC . If I try to return the mutable reference to the last Node , I get a use of moved value error: #[derive(Debug)] struct Node { children: Vec<Node>, } impl Node { fn new(children: Vec<Self>) -> Self { Self { children } } fn get_last(&mut self) -> Option<&mut Node> { self.children.last_mut() } } fn main() { let mut root = Node::new(vec![Node::new(vec![])]); let current = &mut

Python copy.deepcopy() function not working properly [duplicate]

依然范特西╮ 提交于 2019-12-01 22:36:48
This question already has an answer here: How to copy a python class? 8 answers I have been playing with the deepcopy function and the copy function and I get the same issue with both of them. It is like the copy was a reference (or a pointer) instead of a proper copy. I am working with data records (classes) in Python, maybe it could be that.. I show you an example: >>> import copy >>> class player1: ... age = 23 ... score = 1 >>> class player2: ... age = 14 ... score = 2 >>> player3 = copy.deepcopy(player1) I print the parameters. >>> print player1.age, player1.score 23 1 >>> print player2

Encapsulation for mutable objects in Java

好久不见. 提交于 2019-12-01 21:59:00
问题 I was studying the "Java SE 7 Programmer I & II Study Guide" and I did not understand the explanation below. class Fortress{ private String name; private ArrayList<Integer> list; Fortress() {list=new ArrayList<Integer>; String getName{return name;} void addToList(int x){list.add(x);} ArrayList getList(){return list;} // line 1 } Which lines of code break encapsulation? Answer: line 9. "When encapsulating a mutable object like an ArrayList, your getter must return a reference to a copy of the