in Education by
I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I would like to get the following behavior (but I can't manage to find a solution using magics/etc.) : l = obj.a obj.a[0] = 2 print(l) --> [3, 4, 5] print(obj.a) ---> [2, 4, 5] Of course I could simply use copy.deepcopy : l = copy.deepcopy(obj.a) but for several reasons I would like, somehow, to make this step automatic/hide it for my users. [EDIT] Using getattribute and returning a copy won't work of course: import copy class Test: def __init__(self): self.a = [] def __getattribute__(self, attr): if attr == 'a': return copy.deepcopy(super(Test, self).__getattribute__(attr)) Any help appreciated ! Thnak you, Thomas JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It's not possible to make the assignment l = obj.a make a copy of obj.a. As deceze said in a comment, you could make a a property that returns a copy of the value every time you access it, but that would, well, make a copy every time you access it, not just when you assign it to l. That's going to be inefficient, and probably not the behavior you want anyway There's no way for obj or obj.a to tell the difference between something like this: x = obj.a and this: obj.a[:2] Whatever happens when you access obj.a, it's going to happen in both cases. You can't "look ahead" to see whether it's going to be assigned to a variable, just to copy it in that particular case.

Related questions

0 votes
    I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    There is no explanation in python documentation whether parameters are passed by value or reference and why the ... actual reference? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Earlier I used to think that Java is Pass by Reference, but During my latest research iI came to the conclusion that ... and why is it Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    What is the difference between pass by value by reference in c and pass by reference in c?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    How are arguments passed by value or by reference in python?...
asked Dec 7, 2020 in Technology by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I am looking for a way to return the interpolated coordinate value of a variable at a query variable value ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I have the list of sets like so. I basically want to convert this to the dictionary and to address duplicate keys, ... ] = val return d Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me what is the largest value of an int data type in the python? Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I have this dataframe and trying to select the last n (=2) rows if the present value is True so I code as ... , I should select 50,40 Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I wonder what is better to do: d = {'a': 1, 'b': 2} 'a' in d True or: d = {'a': 1, 'b': 2} d.has_key('a') True Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    How can someone parse a numeric string like "121.5555" to it's corresponding float value 121.5555? or parse a ... str to an int. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
...