Is there a reference/alias/pointer in Python?
james at logicalprogression.net
Fri Jul 23 04:45:42 CEST 2004
Daniel Eloff wrote:
> class UserOptionsClass(object): ? what does inheriting from object do?
> Enable you to use properties?
That is correct. Specifically it makes it a new-style class, see:
> Additionally if I return Users['current_users_username'] from a method
> isn't it also ref counted? Probably not since
> Users['current_users_username'] must not return a ref-counted object...
> Python is still all very confusing to me :) Would you do me a big favor
> and help me figure out where to expect a rec-counted instance or a
I can't give you the full story now (assuming I can at all) because it
3.30am my time, but I'm putting the discussion on list so that others
Basically every Python object is a pointer, a PyObject* in the C
implementation. So if you assign a variable to
Users['current_users_username'] you have two references to the same
object. However, you can't use this fact to change the value of one
variable through the other because you can't dereference and change the
value of either. All you can do is make the variable point to a new object.
All this is assuming that the variables are strings, which are immutable
in Python, meaning that they cannot be changed in place. If you have
two indentifiers pointing to the same mutable object, such as a list,
then you can change the value of one through the other by changing it in
place (e.g. assigning to an element of the list) rather than by
re-assigning the variable.
If this doesn't make sense, you know why. :)
More information about the Python-list