Joint ownership? [SOLVED]

I created a custom class that I want to handle all my asset loading and to store things like the file paths, etc.

The way I’m using it right now, it’s instantiated in the core World class and then passed down to classes that need it with the following method:

class MyClass:
     def __init__(self, assetLoader):
          self.assetLoader = assetLoader

My hope and belief is that this results in a single instance of the assetLoader class in memory, and all the classes that I have passed it to possess a reference to that same assetLoader. When I use self.assetLoader in any of them, it all points to the same object in memory.

That is how it works, right?

Right. Python passes everything by-reference (rather than by-value) so you don’t need to worry about implicitly creating a copy of your object in memory. That is, assuming, you don’t call a copy constructor somewhere. (e.g. str(mystr) will create a new copy of mystr.)

Great, that’s just what I wanted to hear, rdb. Thanks.

rdb, i don’t think you are right with str()

>>> a = "hi there"
>>> id(a)
139956567609232
>>> b = str(a)
>>> id(b)
139956567609232

My bad, you’re right. That behavior is only with C++.

>>> a = "hi there"
>>> sys.getrefcount(a) - 1
1
>>> b = str(a)
>>> sys.getrefcount(a) - 1
2