Skip to content
Advertisement

Why does the self in the class always have the previous data?

im new to python and today i had this problem:

say, i have this code incluing two classes named: slice and mat:

JavaScript

output:

JavaScript

I thought that: the list_mat would have 2 mat object(which is true by the output of print(len(list_mat)))

And i thought every mat object in list_mat would have 4 slice objects and the var for each slice obj would be 1 2 3 4 and 5 6 7 8 for each mat obj.

But when it came to the actual mat object in the list_mat,every mat have 8 slice objects and they had the exactly same 8 slice objects in the memory.

When i debugged, i found that when i created the second mat object, in the __init__, the self already had previous value(which were the slices created for the first mat). So that when i passed the second list 5 6 7 8 for the constructor, it would just add another 4 slice objects for the second mat which gave me the result of 8 slice objects.

Another thing is when the second mat was created, the __slice in the first mat changed into the __slice in the second mat! Actually when i checked the __init__ function when debugged, i found that the address of the second mat‘s self object, it was the address of the first mat object which had beed created already.

There must be some deepcopy stuff i have missed.

So to sum:

1: during a for loop, the self obj for the mat class always has the previous data left by the creation of the first mat obj

2: during a for loop, how can i deepcopy and created a whole new obj with everything created new in the memory

Any advice would help!

let me know if you don’t understand my question!

Wishes!

Advertisement

Answer

@jasonharper had it right…..

JavaScript

This code creates a CLASS LEVEL variable which is shared by all instances of the class. This is useful for example, if you wanted to have a object_count variable which you increment during object creation and decrement during destruction. But, in your case, you want each mat object to have its own __slices, so you want to define that variable within __init__():

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement