In C++ you can do the following:
int x = 10;
int &y = x;
y = 11;
cout << x << endl; // will print 11
How can I do something similar in python? Trying to make self.session a reference to self.request.session:
self.session = self.request.session
The following will make both
self.sessionandself.request.sessionrefer to the same object:If you make a change to
sessionvia either of the two references, you’ll be able to observe the change through both:If you, on the other hand, rebind either reference by making it point someplace else, that’ll break the link:
Finally, it is important to note that certain Python types are immutable. This includes integers, strings and tuples. While you can have multiple references to an immutable object, the immutability prevents you from making any changes to that object.
This is relevant in light of your C++ example. If you wanted to have a shared reference to an
int, and be able to modify thatint, you’d have to wrap theintinside a mutable object, and share references to that mutable object instead.