![]() |
Memory Location, Object Attributes and how to use them - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Memory Location, Object Attributes and how to use them (/thread-34694.html) |
Memory Location, Object Attributes and how to use them - Meld51 - Aug-22-2021 Here's something fundamental that I don't understand about the Object Oriented paradigm and I could do with some discussion on this. Obviously I am a novice but I am studying UK A level Computer Science so I'm a year into Python coding. I can easily create a class and provide it with some instance attributes. I can create a number of objects and print a memory location for each one (this shows me that each object is distinct with it's own location). I can print the object attributes. It seems quite common to copy the object attributes into a list or a dictionary for processing elsewhere. What I don't understand is this: How do I alter the object attributes in the object (in memory) rather than in the resulting list? Do I even need to do that? I can't see through this. When I look at example code here and there, I see that the list might be upgraded but that's not the object is it...it's a list! How do I get back to an object in memory and change that? I'm not even sure if I would need to do that but why create an object in memory if you don't want to work on it somewhere along the line. Why not just work on a list in the first place? It is common to save the list of object attributes to a file so they can be reused later but when they are reloaded, are they objects or are they just a list of attributes? I know this is rather fundamental but I don't understand it and I want to. I don't think I need to upload any code for this question but I can provide some if needed. Let me know Thanks in advance RE: Memory Location, Object Attributes and how to use them - Yoriz - Aug-22-2021 The list holds a pointer to the objects they are not copies of the object. class TestClass: def __init__(self, value): self.value = value test_classes = [TestClass(number) for number in range(5)] for test_class in test_classes: print(test_class.value) test_class5 = TestClass(5) test_classes.append(test_class5) print(test_classes[5] is test_class5) print(test_classes[5]) print(test_class5) test_class5.value = 20 print(test_class5.value) print(test_classes[5].value)
|