Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many memory copies do object variables in Python have? [duplicate]

Tags:

java

python

I am new to python recently. Previously all my programming knowledge are limited on Java. So here I have a question about object variables in Python. I know that object variables in Python share on class instances. For example.

class A:
    list=[]

y=A()
x=A()

x.list.append(1)
y.list.append(2)
x.list.append(3)
y.list.append(4)

print x.list
    [1,2,3,4]
print y.list
    [1,2,3,4]

So my questions is that how many memory copies does A.list have? only 1 or just as many as number of instances? object variables in python sharing behavior is just like Java's static class variables, are these two concepts same or different? if different, what's diff between them?

like image 428
JoJo Avatar asked Jan 30 '15 06:01

JoJo


People also ask

How to find out how much memory is being used in Python?

In this article, we are going to see how to find out how much memory is being used by an object in Python. For this, we will use sys.getsizeof () function can be done to find the storage size of a particular object that occupies some space in the memory. This function returns the size of the object in bytes.

Are the memory addresses of variables the same in Python?

Surprisingly, the memory addresses that both variables point to are the same. Look at another example. I defined two variables ( str1 and str2) and assigned a string object ( Python) to both of them. The memory addresses that both variables point to are the same. If you test the same thing for boolean objects, you will see a similar observation.

Does CPython use a lot of memory?

I can’t make much sense out of these results. CPython uses a lot of memory for its objects. It uses various tricks and optimizations for memory management. By keeping track of your object’s memory usage and being aware of the memory management model, you can significantly reduce the memory footprint of your program.

How many bytes does Python take to store an integer?

In the below example it is obvious that Python takes 4 bytes for storing an integer. What happens, in Python, to the original object in memory (and its memory location content) when I change a string (or float, or integer, or other "immutable") during a variable assignment, for example x='first' followed by x='second'?


1 Answers

In python, anything declared at the class scope is effectively global. When you look up that attribute on an instance, python doesn't find it, so it then looks on the class (then it continues looking on the base classes all the way up the method resolution order). So, in your case, x.list is the exact same thing as A.list because there is no instance attribute list attached to x. Similary, y.list is the same as A.list so x.list and y.list refer to the same underlying list. (Phew!)

As I understand it, this is at least similar to Java's static (though I'm not fluent enough in Java to say exactly how similar).

The typical way to disassociate an attribute from the class is to bind that attribute to an instance (usually in __init__):

class A(object):
    def __init__(self):
        self.list = []

In the above example, self is the instance and it gets passed (implicitly) to any 'normal' method (including "magic" methods like __init__).

Now if you do your experiment again, you'll see that x.list takes the value of [1, 3] and y.list is now [2, 4].


Now the test. What happens if you do your experiment on this class?

class A(object):
    list = []
    def __init__(self):
        self.list = []

Answer: x.list = [1, 3] and y.list = [2, 4]. The reason is because when python does x.list, it looks on the instance first (before looking at the class). Since an attribute with the name list is found on the instance, that is the one that is used.

like image 186
mgilson Avatar answered Oct 23 '22 22:10

mgilson