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

Brian van den Broek broek at cc.umanitoba.ca
Tue May 22 19:53:33 EDT 2007


Stef Mientki said unto the world upon 05/22/2007 07:44 PM:
> hello,
> 
> I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs).
> My first action is to translate the JAL code into Python code.
> The reason for this approach is that it simplifies the simulator very much.
> In this translation I want to keep the JAL-syntax as much as possible intact,
> so anyone who can read JAL, can also understand the Python syntax.
> 
> One of the problems is the alias statement, assigning a second name to an object.
> I've defined a class IO_port,
> and I create an instance of that port with the name port_D
> 
>      port_D = IO_port('D')
> 
> 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.
> 
> thanks,
> Stef Mientki
> 

Stef,

You can have multiple names pointing to an object. Consider:

 >>> class Example(object):
...   pass
...
 >>> c1 = Example()
 >>> c2 = c1
 >>> id(c1), id(c2)
(136764076, 136764076)
 >>> c1, c2
(<__main__.Example object at 0x826daac>, <__main__.Example object at 
0x826daac>)
 >>> del(c2)
 >>> c1, c2
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'c2' is not defined
 >>> c1
<__main__.Example object at 0x826daac>
 >>>

HTH,

Brian vdB



More information about the Python-list mailing list