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

Eric Yaeger ericy22 at gmail.com
Wed May 23 09:19:05 EDT 2007


Perhaps Stef Mientki, you might be interested in copy.copy( )  and
copy.deepcopy( ) ! Please see the info I have put below.

On May 23, 12:44 am, Stef Mientki <S.Mientki-nos... at mailbox.kun.nl>
wrote:
> 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')

> thanks,
> Stef Mientki
>
>


====================================================
The following text is an excerpt from Chapter 12 of book:

How to Think Like a Computer Scientist
Learning with Python
by Allen B. Downey, Jeffrey Elkner and Chris Meyers
[Book available for free download at http://www.thinkpython.com]




=.=.=.=.=.=.=.=.=.=
Chapter 12

Classes and objects

[...]
[...]

12.4 Sameness

The meaning of the word "same" seems perfectly clear until you give it
some thought, and then you realize there is more to it than you
expected.

For example, if you say, "Chris and I have the same car," you mean
that his car and yours are the same make and model, but that they are
two different cars. If you say, "Chris and I have the same mother,"
you mean that his mother and yours are the same person. [...] So the
idea of "sameness" is different depending on the context.

When you talk about objects, there is a similar ambiguity. For
example, if two Points are the same, does that mean they contain the
same data (coordinates) or that they are actually the same object?

To find out if two references refer to the same object, use the ==
operator. For example:

>>> p1 = Point()
>>> p1.x = 3
>>> p1.y = 4
>>> p2 = Point()
>>> p2.x = 3
>>> p2.y = 4
>>> p1 == p2
0


Even though p1 and p2 contain the same coordinates, they are not the
same object. If we assign p1 to p2, then the two variables are aliases
of the same object:

>>> p2 = p1
>>> p1 == p2
1


This type of equality is called shallow equality because it compares
only the references, not the contents of the objects.

To compare the contents of the objects [use] deep equality... ...
[...]
[...]


12.8 Copying
[...]
[...]


12.9 Glossary

class - A user-defined compound type. A class can also be thought of
as a template for the objects that are instances of it.

instantiate - To create an instance of a class.

instance - An object that belongs to a class.

object - A compound data type that is often used to model a thing or
concept in the real world.

constructor - A method used to create new objects.

attribute - One of the named data items that makes up an instance.

shallow equality - Equality of references, or two references that
point to the same object.

deep equality - Equality of values, or two references that point to
objects that have the same value.

shallow copy - To copy the contents of an object, including any
references to embedded objects; implemented by the copyfunction in the
copy module. Example:
>>> b2 = copy.copy(b1)

deep copy - To copy the contents of an object as well as any embedded
objects, and any objects embedded in them, and so on; implemented by
the deepcopy function in the copy module. Example:
>>> b2 = copy.deepcopy(b1)




=.=.=.=.=.=.=.=.=.=
The preceding text is an excerpt from Chapter 12 of book:

How to Think Like a Computer Scientist
Learning with Python
by Allen B. Downey, Jeffrey Elkner and Chris Meyers
[Book available for free download at http://www.thinkpython.com]


=====================================================

Bye from,

Eric Yaeger
Thailand





More information about the Python-list mailing list