Standard behaviour of a getSomething method

B

Batista, Facundo

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

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

How do I return a copy of the object?

Thanks for all.

.. Facundo





.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .
.. . . . . . . . . . . . . . .
ADVERTENCIA

La información contenida en este mensaje y cualquier archivo anexo al mismo,
son para uso exclusivo del destinatario y pueden contener información
confidencial o propietaria, cuya divulgación es sancionada por la ley.

Si Ud. No es uno de los destinatarios consignados o la persona responsable
de hacer llegar este mensaje a los destinatarios consignados, no está
autorizado a divulgar, copiar, distribuir o retener información (o parte de
ella) contenida en este mensaje. Por favor notifíquenos respondiendo al
remitente, borre el mensaje original y borre las copias (impresas o grabadas
en cualquier medio magnético) que pueda haber realizado del mismo.

Todas las opiniones contenidas en este mail son propias del autor del
mensaje y no necesariamente coinciden con las de Telefónica Comunicaciones
Personales S.A. o alguna empresa asociada.

Los mensajes electrónicos pueden ser alterados, motivo por el cual
Telefónica Comunicaciones Personales S.A. no aceptará ninguna obligación
cualquiera sea el resultante de este mensaje.

Muchas Gracias.
 
L

Lee Harr

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
When I want to know about a attribute (e.g.: myAttrib) of an object, I
should use the specific method (e.g.: getMyAttrib).

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

I do not think there is a definite rule regarding this. However,
whichever one it does, you should probably document it :eek:)


How do I return a copy of the object?

You can use the copy module to create either a shallow or deep copy.
 
D

Dan Williams

When I want to know about a attribute (e.g.: myAttrib) of an object, I
should use the specific method (e.g.: getMyAttrib).
Its not really considered "pythonic" to access attributes through
getters and setters. Instead, you can access them directly:
.... def __init__(self, val):
.... self.a = val
....
If you need to to other processing of the data for the data (ie, if you
wanted to give a copy), you can use the __getattr__ and __setattr__
methods.
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

How do I return a copy of the object? try looking at:
HTH,
-Dan


Thanks for all.

. Facundo
[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top