2

I have been unable to find the answer to this on the Python documentation page (https://docs.python.org/2/tutorial/classes.html). I need to know this because I am working with large objects, and this would affect whether it is feasible to have a large object as a class variable. For example:

class LargeList:
 def __init__(self, mylist):
 self.mylist = mylist
if __name__ == '__main__':
 x = range(0, 1000000000)
 largelist = LargeList(x)

Would this object store a pointer to x?

JAB
21.2k6 gold badges73 silver badges80 bronze badges
asked Aug 27, 2015 at 14:14
3
  • 1
    Yes, it would - the list object created by range would be accessible via largelist.mylist. They're generally referred to as "references" in Python, rather than "pointers", although the effect is much the same. Commented Aug 27, 2015 at 14:16
  • 1
    Actually is is instance variable not class. Commented Aug 27, 2015 at 14:20
  • But do also note that in CPython, a "reference" is a pointer to a PyObject-like struct defined in the C source (or in your own extension module). The syntax class Foo ... is interpreted by executing the necessary C machinery to define a new PyObject-like type. Further, and probably more relevant here, in Python, a basic list is really a contiguous array of pointers and it is not a pointer to a contiguous array of memory locations. When a list is constructed, it might be contiguous in memory too, but there's no guarantee it won't be fragmented, or become fragmented as items change. Commented Aug 27, 2015 at 14:22

2 Answers 2

4

You would have a single copy of the large list, with two references to it: the variable x in the module scope, and self.mylist in the scope of the class.

answered Aug 27, 2015 at 14:17
Sign up to request clarification or add additional context in comments.

Comments

0

Nope, Python class variables are not pointers. But since largelist.mylist is pointing to a list, the behaviour of largelist.mylist will be like pointers.

This happens in Python with mutable types - e.g. - list, dict.

If larglist.mylist is pointing to a string, then the behaviour will be totally different since str is an immutable type i.e. it can't be mutated.

See this section of Python docs for more info

answered Aug 27, 2015 at 14:25

3 Comments

This isn't exactly correct but it's a good approximation. It would still be a pointer in the case of a string, just pointing to a CPython implemented PyObject-like struct embodying the implementation of Python strings. The fact that this "is immutable" would just be a pure convention of what function pointers that PyObject-like implementation happens to have, and whether or not they happen to raise exceptions when certain operations are attempted, instead of actually carrying out the operations. So under the hood it's still a pointer to something, just not a pointer to a raw C-level value
This would also be the case with user defined types, since they are also mutable, right?
@Alexander Whatley Yeah.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.