Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Standard behaviour of a getSomething method

Reply
Thread Tools

Standard behaviour of a getSomething method

 
 
Batista, Facundo
Guest
Posts: n/a
 
      07-23-2003
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.

 
Reply With Quote
 
 
 
 
Lee Harr
Guest
Posts: n/a
 
      07-23-2003
In article <mailman.1058966922.4133.python->, Batista, Facundo:
> 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 )



> How do I return a copy of the object?
>


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


 
Reply With Quote
 
 
 
 
Dan Williams
Guest
Posts: n/a
 
      07-23-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).
>

Its not really considered "pythonic" to access attributes through
getters and setters. Instead, you can access them directly:

>>> class Foo:

.... def __init__(self, val):
.... self.a = val
....
>>> foo = Foo(4)
>>> print foo.a

4
>>> foo.a = 6
>>> print foo.a

6
>>>


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:
>>> import copy
>>> help(copy)


HTH,
-Dan

>
> Thanks for all.
>
> . Facundo
>
>

[snip]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
pre, post increment standard behaviour, and friend function declaration eddiew_AUS C++ 18 01-27-2004 04:31 PM
Standard behaviour ? Ares Lagae C++ 4 11-26-2003 05:26 PM
Irregular behaviour: C++ standard lib and file stream. bruce varley C++ 2 11-25-2003 05:27 PM
Standard behaviour of a getSomething method Batista, Facundo Python 0 07-24-2003 09:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57