Standard behaviour of a getSomething method

Mike Rovner mike at nospam.com
Thu Jul 24 17:56:24 EDT 2003


Batista, Facundo wrote:

> When I want to know about a attribute (e.g.: myAttrib) of an object, I
> should use the specific method (e.g.: getMyAttrib).

Python don't enforce nor encourage attribute hiding,
so don't do it unless you have a good reason.
Plain access to attributes is simple:
    myObject.myAttribute

> Considering that this attribute is always another object (everything
> is an object in Python), what should getMyAttrib do?
>
> 1) Return the object
> 2) Return a copy of the object

Usualy statement
   x=y
creates a new object y referencing to old object y.
If y allows modification it can be modified through name 'x'
If you want a copy:

> How do I return a copy of the object?

Do it explicitly:
   myObject.copy()
or
  import copy
  copy.copy(myObject)
  copy.deepcopy(myObject)

Mike








More information about the Python-list mailing list