Re: replace a method in class: how?

Discussion in 'Python' started by Maric Michaud, Jun 27, 2006.

  1. Le mardi 27 juin 2006 01:14, Brian Blais a écrit :
    > t=This()
    > t.update(5)
    > t.update=another_update
    > t.update(5)  # this one doesn't work, gives
    > # TypeError: another_update() takes exactly 2 arguments (1 given)
    >
    >
    > clearly it isn't seeing it as a method, just an attribute which happens to
    > be a function.  Is there a preferred way to do this?


    Yes, this is because you assigned it to the instance, not the class, it should
    be :
    t=This()
    t.update(5)
    This.update=another_update
    t.update(5)

    In OOP Methods are defined in *classes* not in any arbitrary object (ie.
    instances). You can learn more on how it works in python reading about the
    descriptor protocol of new style class (__getattribute__ special method).

    To clearly understand what belongs to class and what belongs to instances,
    try :
    u=This()
    t.prop = None
    t.prop
    u.prop
    This.other_prop = None
    t.other_prop, u.other_prop
    This.__dict__.items()
    t.__dict__, u.__dict__


    --
    _____________

    Maric Michaud
    _____________

    Aristote - www.aristote.info
    3 place des tapis
    69004 Lyon
    Tel: +33 426 880 097
     
    Maric Michaud, Jun 27, 2006
    #1
    1. Advertising

  2. Le mardi 27 juin 2006 06:21, Bruno Desthuilliers a écrit :
    > Maric Michaud a écrit :
    > (snip)
    >
    > > In OOP Methods are defined in *classes* not in any arbitrary object

    >
    > Chapter and verse, please ? AFAIK, the first O in OOP stands for
    > "object", not for "class" !-)
    >

    Hard to find it, indeed.

    > Classes are just an implementation convenience, and the fact that the
    > class-based model is the most usual one doesn't imply it's the only
    > valid one.

    Maybe, never been told of another one, do you think of javascript prototypes ?
    Well, there are no methods in this model, only functions.

    > So there's no reason one shouldn't override (or add) a method
    > on a per-object basis. As a matter of fact, it's perfectly legal (and
    > can be very convenient) to do so in Python.

    I never saw the term "method" used for anything except for what it means for
    classes, and moreover, the class model, even if it's just an implementation,
    it's the one chosen by python.
    For the common understanding of the model, IMHO, classes bind methods,
    instances bind functions.

    --
    _____________

    Maric Michaud
    _____________

    Aristote - www.aristote.info
    3 place des tapis
    69004 Lyon
    Tel: +33 426 880 097
     
    Maric Michaud, Jun 27, 2006
    #2
    1. Advertising

  3. Maric Michaud a écrit :
    (snip)

    > In OOP Methods are defined in *classes* not in any arbitrary object


    Chapter and verse, please ? AFAIK, the first O in OOP stands for
    "object", not for "class" !-)

    Classes are just an implementation convenience, and the fact that the
    class-based model is the most usual one doesn't imply it's the only
    valid one. So there's no reason one shouldn't override (or add) a method
    on a per-object basis. As a matter of fact, it's perfectly legal (and
    can be very convenient) to do so in Python.
     
    Bruno Desthuilliers, Jun 27, 2006
    #3
  4. Maric Michaud wrote:
    > Le mardi 27 juin 2006 06:21, Bruno Desthuilliers a écrit :
    >
    >>Maric Michaud a écrit :
    >>(snip)
    >>
    >>
    >>>In OOP Methods are defined in *classes* not in any arbitrary object

    >>
    >>Chapter and verse, please ? AFAIK, the first O in OOP stands for
    >>"object", not for "class" !-)
    >>

    >
    > Hard to find it, indeed.
    >
    >
    >>Classes are just an implementation convenience, and the fact that the
    >>class-based model is the most usual one doesn't imply it's the only
    >>valid one.

    >
    > Maybe, never been told of another one, do you think of javascript prototypes ?


    There other prototype-based languages. And truth is that while having
    classes, Python's object model is much more closer to javascript's one
    than to Java or C++.

    > Well, there are no methods in this model, only functions.


    Nope. There are closures, not simple functions.

    And FWIW, what do you think methods are in Python ? Yes, functions,
    wrapped in a special descriptor.

    >
    >>So there's no reason one shouldn't override (or add) a method
    >>on a per-object basis. As a matter of fact, it's perfectly legal (and
    >>can be very convenient) to do so in Python.

    >
    > I never saw the term "method" used for anything except for what it means for
    > classes,


    Help on class instancemethod in module __builtin__:

    class instancemethod(object)
    | instancemethod(function, instance, class)
    |
    | Create an instance method object.


    > and moreover, the class model, even if it's just an implementation,
    > it's the one chosen by python.


    There's a superficial similarity with "mainstream" static OOPLs like
    Java/C++ etc. But Python's classes, and their relation to instances, are
    as dynamic as can be, and really very different from what's in most books.

    > For the common understanding of the model, IMHO, classes bind methods,
    > instances bind functions.


    ???

    In Python, a class is nothing more than an object that:
    1/ act as an object factory
    2/ serves as delegatee for instances.

    FWIW, the proper constructor (__new__, not __init__) is free to return
    instances of any other class (as long as the initializer signature is
    compatible).


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    Bruno Desthuilliers, Jun 27, 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. E11
    Replies:
    1
    Views:
    4,783
    Thomas Weidenfeller
    Oct 12, 2005
  2. Brian Blais
    Replies:
    1
    Views:
    382
    Bruno Desthuilliers
    Jun 27, 2006
  3. Greg Ewing
    Replies:
    2
    Views:
    345
    Dieter Maurer
    Jun 29, 2006
  4. Pawel_Iks
    Replies:
    3
    Views:
    536
    terminator
    Jul 31, 2007
  5. Replies:
    3
    Views:
    929
    Roedy Green
    Jan 28, 2008
Loading...

Share This Page