Python Forum
Memory Location, Object Attributes and how to use them
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memory Location, Object Attributes and how to use them
#1
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
Reply
#2
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)
Output:
0 1 2 3 4 True <__main__.TestClass object at 0x000001F1B4CD8B20> <__main__.TestClass object at 0x000001F1B4CD8B20> 20 20
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find location of opengl object? hsunteik 1 3,316 Jan-17-2017, 11:30 AM
Last Post: Larz60+
  quick question about deleting an object from memory LavaCreeperKing 5 5,769 Nov-12-2016, 04:05 PM
Last Post: LavaCreeperKing

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020