Assigning to self.__class__

Discussion in 'Python' started by Paul McGuire, Jan 26, 2006.

  1. Paul McGuire

    Paul McGuire Guest

    I have some places in pyparsing where I've found that the most
    straightforward way to adjust an instance's behavior is to change its class.
    I do this by assigning to self.__class__, and things all work fine.

    (Converting to use of __new__ is not an option - in one case, the change is
    temporary, and before the end of the function, I change it back again.)

    Any comments on this practice? Is this intended capability for Python
    objects, or am I taking advantage of a fortuitous accident, which may get
    undone at a future time?

    -- Paul
    Paul McGuire, Jan 26, 2006
    #1
    1. Advertising

  2. Paul McGuire wrote:
    > I have some places in pyparsing where I've found that the most
    > straightforward way to adjust an instance's behavior is to change its class.


    Hooray ! You've just (re)discovered the state pattern... for which the
    most stupid simple implementation in Python is to :

    >(snip) assigning to self.__class__, (snip)


    !-)

    >
    > Any comments on this practice?


    It can be very confusing for newbies and peoples having no experience
    with *dynamic* languages, and I guess control-freaks and
    static-typing-addicts would runaway screaming. But I like it anyway !-)

    > Is this intended capability for Python
    > objects,


    AFAIK, yes.

    > or am I taking advantage of a fortuitous accident, which may get
    > undone at a future time?


    It's certainly not a fortuitous accident.


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno at modulix, Jan 26, 2006
    #2
    1. Advertising

  3. bruno at modulix wrote:
    > Paul McGuire wrote:
    >> or am I taking advantage of a fortuitous accident, which may get
    >> undone at a future time?

    >
    > It's certainly not a fortuitous accident.


    And even the (printed) cookbook has examples which assign to
    self.__class__... I guess this means this feature isn't going to go away
    soo. ;-)

    --- Heiko.
    Heiko Wundram, Jan 26, 2006
    #3
  4. Heiko Wundram <> wrote:

    > bruno at modulix wrote:
    > > Paul McGuire wrote:
    > >> or am I taking advantage of a fortuitous accident, which may get
    > >> undone at a future time?

    > >
    > > It's certainly not a fortuitous accident.

    >
    > And even the (printed) cookbook has examples which assign to
    > self.__class__... I guess this means this feature isn't going to go away


    Hmmm, I'm the main decision-maker for what goes in the printed cookbook,
    but it's Guido who decides what stays or goes in the language, and it's
    not as if we always agree (though I will admit that when we disagree it
    later usually turns out he's right, but that's another issue;-). Still,
    in this case I don't think we disagree!-)


    Alex
    Alex Martelli, Jan 26, 2006
    #4
  5. Paul McGuire

    Terry Reedy Guest

    "Paul McGuire" <._bogus_.com> wrote in message
    news:_G%Bf.1219$...
    >I have some places in pyparsing where I've found that the most
    > straightforward way to adjust an instance's behavior is to change its
    > class.
    > I do this by assigning to self.__class__, and things all work fine.
    >
    > (Converting to use of __new__ is not an option - in one case, the change
    > is
    > temporary, and before the end of the function, I change it back again.)
    >
    > Any comments on this practice? Is this intended capability for Python
    > objects, or am I taking advantage of a fortuitous accident, which may get
    > undone at a future time?


    Very little if any of the new class stuff is accidental. As I remember,
    ..__class__ was originally read-only and was intentionally changed when
    practical use cases for rewriting it were developed and presented with a
    request for change. Though I could be wrong, I would comfortably use this
    feature myself.

    Terry J. Reedy
    Terry Reedy, Jan 26, 2006
    #5
    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. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    580
    Lonnie Princehouse
    Jul 11, 2005
  2. Replies:
    1
    Views:
    409
  3. Gabriel Rossetti

    ClassName.attribute vs self.__class__.attribute

    Gabriel Rossetti, Jun 5, 2008, in forum: Python
    Replies:
    14
    Views:
    710
    Terry Reedy
    Jun 13, 2008
  4. Dmitry Ponyatov
    Replies:
    1
    Views:
    472
    James Mills
    Apr 21, 2010
  5. kj
    Replies:
    4
    Views:
    1,005
    Steven D'Aprano
    Dec 3, 2010
Loading...

Share This Page