Can I reference 1 instance of an object by more names ?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed May 23 01:13:55 EDT 2007


On Wed, 23 May 2007 01:44:22 +0200, Stef Mientki wrote:

> Now I want to assign a more logical name to that port, (In JAL: "var
> byte My_New_Name IS port_D")
> 
> Is that possible ?
> 
> I think the answer is "no",
> because the object itself is not mutable. Am I right ?
> 
> But I read: "An object can have any number of names, or no name at all."
> So am I wrong ?
> 
> Sorry this has been discussed before, but I'm totally confused.


# oops, we misspelled a name in the official API
# can't change it, so alias it
mispeled_name = IO_port('D')
fixed_name = IO_port('D')
correct_name = mispeled_name

You can test if two names point to the same object with the is operator:

>>> mispeled_name is correct_name
True
>>> mispeled_name is fixed_name
False



But make sure you really care that they are the same object. In general, 
testing for equality is more sensible.

>>> 1 == 1.0
True
>>> 1 is 1.0
False
>>> "hello world" == ("hello " + "world")
True
>>> "hello world" is ("hello " + "world")
False



-- 
Steven.



More information about the Python-list mailing list