Is there a reference/alias/pointer in Python?
James Henderson
james at logicalprogression.net
Thu Jul 22 22:45:42 EDT 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:
http://www.python.org/doc/newstyle.html
> 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
> reference?
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
can contribute.
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. :)
James
More information about the Python-list
mailing list