473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Standard behaviour of a getSomething method

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.

Jul 18 '05 #1
2 1320
In article <ma*********************************@python.org> , 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 :o)
How do I return a copy of the object?


You can use the copy module to create either a shallow or deep copy.
Jul 18 '05 #2
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]

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Nicolas Lehuen | last post by:
Hi, I hope this is not a FAQ, but I have trouble understanding the behaviour of the super() built-in function. I've read the excellent book 'Python in a Nutshell' which explains this built-in...
4
by: Ares Lagae | last post by:
Could someone please tell me what the behaviour of the following code snippet is, according to the C++ standard ? -------------------------------------------------------------------------- ...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
16
by: subramanian100in | last post by:
Program 1: --------------- #include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.