Standard behaviour of a getSomething method

Discussion in 'Python' started by Batista, Facundo, Jul 23, 2003.

  1. 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.
    Batista, Facundo, Jul 23, 2003
    #1
    1. Advertising

  2. Batista, Facundo

    Lee Harr Guest

    In article <>, 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 :eek:)



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


    You can use the copy module to create either a shallow or deep copy.
    Lee Harr, Jul 23, 2003
    #2
    1. Advertising

  3. Batista, Facundo

    Dan Williams Guest

    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]
    Dan Williams, Jul 24, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bruce varley
    Replies:
    2
    Views:
    395
    Mike Wahler
    Nov 25, 2003
  2. Ares Lagae

    Standard behaviour ?

    Ares Lagae, Nov 26, 2003, in forum: C++
    Replies:
    4
    Views:
    410
    Ares Lagae
    Nov 26, 2003
  3. eddiew_AUS
    Replies:
    18
    Views:
    642
    Andrey Tarasevich
    Jan 27, 2004
  4. Batista, Facundo

    Standard behaviour of a getSomething method

    Batista, Facundo, Jul 24, 2003, in forum: Python
    Replies:
    0
    Views:
    263
    Batista, Facundo
    Jul 24, 2003
  5. Andy Chambers
    Replies:
    1
    Views:
    384
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page