Re: intX.__str__() ??

Discussion in 'Python' started by Mitya Sirenef, Feb 24, 2013.

  1. On 02/24/2013 12:29 PM, Michael Torrie wrote:
    >
    > I think he's maintaining existing code. It's unfortunate that his first
    > exposure to python is code written by someone else in such a poor style,
    > and in a way that definitely isn't pythonic. No wonder he's struggling
    > to like python! Though I'm sure since his recent experience has been
    > exclusively in C# that he probably uses hungarian notation as a matter
    > of course. A hard habit to break! Is this a good time to introduce him
    > to duck typing? Probably not.
    >
    > Another way to explain the double underscore methods is that they are
    > how things like operator overloading is performed. Want to make a class
    > that you can use the [index] notation on instances? Define the
    > __get_attr__() method. And to define a class that you can then use


    small correction: it should be __getattr__()


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/

    Admiration for a quality or an art can be so strong that it deters us from
    striving to possess it. Friedrich Nietzsche
    Mitya Sirenef, Feb 24, 2013
    #1
    1. Advertising

  2. On Sun, 24 Feb 2013 13:09:56 -0500, Mitya Sirenef wrote:

    > On 02/24/2013 12:29 PM, Michael Torrie wrote:


    > > Another way to explain the double underscore methods is that they are
    > > how things like operator overloading is performed. Want to make a
    > > class that you can use the [index] notation on instances? Define the
    > > __get_attr__() method. And to define a class that you can then use

    >
    > small correction: it should be __getattr__()



    Correct correction: it should be __getitem__ to override instance[item]
    access. __getattr__ is for overriding instance.attribute.


    --
    Steven
    Steven D'Aprano, Feb 24, 2013
    #2
    1. Advertising

  3. On 02/24/2013 01:23 PM, Steven D'Aprano wrote:
    > On Sun, 24 Feb 2013 13:09:56 -0500, Mitya Sirenef wrote:
    >
    >> On 02/24/2013 12:29 PM, Michael Torrie wrote:

    >
    >> > Another way to explain the double underscore methods is that they are
    >> > how things like operator overloading is performed. Want to make a
    >> > class that you can use the [index] notation on instances? Define the
    >> > __get_attr__() method. And to define a class that you can then use

    >>
    >> small correction: it should be __getattr__()

    >
    >
    > Correct correction: it should be __getitem__ to override instance[item]
    > access. __getattr__ is for overriding instance.attribute.
    >
    >



    Oh, yes.. I guess it pays to read more than a single sentence
    when making corrections! -m


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/

    Each religion, by the help of more or less myth, which it takes more or
    less seriously, proposes some method of fortifying the human soul and
    enabling it to make its peace with its destiny. George Santayana
    Mitya Sirenef, Feb 24, 2013
    #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. Jim Newton

    how to use __str__ and __repr__?

    Jim Newton, Jun 7, 2004, in forum: Python
    Replies:
    15
    Views:
    534
    Peter Maas
    Jun 9, 2004
  2. Dan Sommers
    Replies:
    3
    Views:
    297
    Serge Orlov
    Feb 19, 2005
  3. Ethan Furman

    Re: intX.__str__() ??

    Ethan Furman, Feb 24, 2013, in forum: Python
    Replies:
    0
    Views:
    84
    Ethan Furman
    Feb 24, 2013
  4. Michael Torrie

    Re: intX.__str__() ??

    Michael Torrie, Feb 24, 2013, in forum: Python
    Replies:
    0
    Views:
    105
    Michael Torrie
    Feb 24, 2013
  5. Ethan Furman

    Re: intX.__str__() ??

    Ethan Furman, Feb 24, 2013, in forum: Python
    Replies:
    1
    Views:
    73
    Roy Smith
    Feb 24, 2013
Loading...

Share This Page