Any way of adding methods/accessors to built-in classes?

Discussion in 'Python' started by Kenneth McDonald, Oct 25, 2006.

  1. This is possible with pure Python classes. Just add the method as new
    attribute of the class. However, that won't work for the builtins.

    I know that this is somewhat dangerous, and also that I could subclass
    the builtins, but not being able to do things like '[1,2,3]'.length
    drives me a little nuts. Python is about the only computer language I
    use, and I think it's certainly the best of the scripting languages, but
    there are inconsistencies in the object model and some other things I
    wish I could fix. If could could modify the builtins this way, I'd be
    willing to take the risk.

    Thanks,
    Ken
    Kenneth McDonald, Oct 25, 2006
    #1
    1. Advertising

  2. Kenneth McDonald

    Guest

    Kenneth McDonald:
    > not being able to do things like '[1,2,3]'.length
    > drives me a little nuts.


    This is interesting, why?
    (In a computer language too much purity is often bad. And isn't
    [1,2,3].len better?)

    I think you can't add methods to Python builtin classes, I think you
    can do it with Ruby.

    Bye,
    bearophile
    , Oct 25, 2006
    #2
    1. Advertising

  3. Kenneth McDonald

    Paddy Guest

    Kenneth McDonald wrote:
    > This is possible with pure Python classes. Just add the method as new
    > attribute of the class. However, that won't work for the builtins.
    >
    > I know that this is somewhat dangerous, and also that I could subclass
    > the builtins, but not being able to do things like '[1,2,3]'.length
    > drives me a little nuts. Python is about the only computer language I
    > use, and I think it's certainly the best of the scripting languages, but
    > there are inconsistencies in the object model and some other things I
    > wish I could fix. If could could modify the builtins this way, I'd be
    > willing to take the risk.
    >
    > Thanks,
    > Ken


    Hi Ken,
    The reason comes from code being read much more than it is written. It
    makes it much easier to maintain code if some foundations i.e. the
    builtins, don't change. That way everyone maintaining the code will
    know that an int is an int, is an int, wherever it may be in the source
    files of your program. The flip-side is that someone reading List in
    athers code knows it isn't the built-in list type and so is on-guard
    for any deviations/additions.

    As for your irritation Ken, if you have to maintain code, as I do/have
    done, then you remember how many times little inconsistencies have
    tripped you up in the past, and offset those gripes against things like
    this.

    - cheers, Paddy.
    Paddy, Oct 25, 2006
    #3
  4. Kenneth McDonald

    Neil Cerutti Guest

    On 2006-10-25, <> wrote:
    > Kenneth McDonald:
    >> not being able to do things like '[1,2,3]'.length
    >> drives me a little nuts.

    >
    > This is interesting, why?
    > (In a computer language too much purity is often bad. And isn't
    > [1,2,3].len better?)
    >
    > I think you can't add methods to Python builtin classes, I
    > think you can do it with Ruby.


    You can create derived versions of builtins with new methods
    (which affords the benefits of modifying builtins), but
    you can't change the type of a Python literal. So the above would
    be

    MyList([1,2,3]).len

    --
    Neil Cerutti
    Neil Cerutti, Oct 25, 2006
    #4
    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. Alan Ning
    Replies:
    2
    Views:
    364
    Marcus Kwok
    Mar 2, 2006
  2. John Wilger
    Replies:
    3
    Views:
    162
    Dave Thomas
    Nov 13, 2004
  3. Moses Hohman
    Replies:
    6
    Views:
    170
    Moses Hohman
    Nov 16, 2004
  4. Frodo Larik

    Dynamic methods / accessors

    Frodo Larik, Aug 28, 2005, in forum: Ruby
    Replies:
    3
    Views:
    138
    Frodo Larik
    Aug 28, 2005
  5. Kenneth McDonald
    Replies:
    5
    Views:
    299
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page