Re: Dynamically replacing an objects __class__; is it safe?
On Thursday, March 16, 2017 at 7:07:17 PM UTC-7, Deborah Swanson wrote: > Steve D'Aprano wrote,on March 16, 2017 5:07 AM > > > > On Thu, 16 Mar 2017 09:03 am, Gregory Ewing wrote: > > > > > Steve D'Aprano wrote: > > >> You probably can't make a whale fly just by changing the class to > > >> bird. It will need wings, and feathers, at the very least. > > > > > > Some things succeed in flying with neither wings nor feathers. > > > Helicopters, for example. > > > > For some definition of "no wings". > > > > But regardless of rockets, balloons, helicopters, Boeing 747s > > and Dr Strange's mystical Cloak Of Levitation, *birds* > > require wings and feathers to fly. If you just paint "BIRD" > > on the side of a whale, it won't get off the ground, and if > > you do manage to get it airborne (via a catapult, > > perhaps) it will just come down with a rather large splat. > > > > The point is that dynamically swapping the class of an > > existing instance at runtime is *not* just a way of doing > > duck-typing. It really does matter if your duck flies by > > flapping feathered wings or by blasting an exhaust of hot > > gasses out of its rear end at high speed. > > > > With duck-typing, you don't care whether you have a duck or a > > goose, so long as they both can fly: you don't care *how* it > > flies, so long as it does, and even a rocket-propelled > > balloon will be fine. > > > > But when you dynamically swap out __class__ (I think > > Objective-C "swizzling" is conceptually equivalent, so I'll > > call it by that word) you have to care about the > > implementation. The whole point of swizzling is that it > > allows you to swap out one implementation ("run forward and > > flap your wings") with another implementation ("light the > > blue touch paper"). But if you do that, you have to care > > about implementation details. There's no point in setting > > your swizzled fly() method to that of Rocket if your instance > > doesn't have blue touch paper to light. > > > > With duck-typing, making the methods work isn't your > > responsibility. But when you swizzle, you are responsible for > > making sure that the instance provides whatever the methods > > need to work. > > > > A very old but good example of Python swizzling is here: > > http://code.activestate.com/recipes/68429-ring-buffer/ > > -- > Steve > "Cheer up," they said, "things could be worse." So I cheered up, and > sure enough, things got worse. > > Condolences, Steve. Nobody on this thread wants to plumb the depths of > Python swizzling. Or anything else Python, so it appears. Hey Deborah, If you're getting discouraged by a certain spammer who posts to this newsgroup in ALL CAPS and ITALIAN, don't let him beat you. Go find newsreader software that allows you to define filters and killfiles. It will help. If you read further into that article about the RingBuffer class and "swizzling", Steve Alexander suggests a method that I have used myself, and which I consider to be generally superior: conditionally rebinding the names of methods you want to change within a class, rather than overriding __class__ itself. I suppose that each approach has its merits. If you want the object's type to advertise that a condition has changed, rebinding __class__ is good. If you have many methods to change, and you don't like long lists of similarly-named functions in one class, rebinding __class__ could also be good. I don't encounter those conditions much myself. In the RingBufferFull case, only two methods need to change, .append() and .get(). Both methods are short. Both methods' eventual replacements, which we might name ._full_append() and ._get(), are also short. I would do it Steve Alexander's way. -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Sat, Mar 18, 2017 at 8:24 AM,wrote: > On Thursday, March 16, 2017 at 9:27:56 PM UTC-7, Gregory Ewing wrote: >> Chris Angelico wrote: >> > Maybe what the ISS does isn't flying - it's falling with style? >> >> Yep. They didn't really launch it into orbit with rockets, >> that was all faked. They actually hauled it up there with >> a crane, let it go and pulled the earth away at the last >> moment. >> >> -- >> Greg > > Chris, this is evidence you need to stick to Flying Circus references when > attempting to construct a joke in this newsgroup. Buzz Lightyear won't cut > it. And cue the discussion of whether Joke.__init__() is the constructor or not. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Thursday, March 16, 2017 at 9:27:56 PM UTC-7, Gregory Ewing wrote: > Chris Angelico wrote: > > Maybe what the ISS does isn't flying - it's falling with style? > > Yep. They didn't really launch it into orbit with rockets, > that was all faked. They actually hauled it up there with > a crane, let it go and pulled the earth away at the last > moment. > > -- > Greg Chris, this is evidence you need to stick to Flying Circus references when attempting to construct a joke in this newsgroup. Buzz Lightyear won't cut it. -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On 17/03/17 04:18, Gregory Ewing wrote: Dennis Lee Bieber wrote: I'd say satellites do "not" fly, as they have no force/action opposing the fall caused by the pull of gravity. Arrows, bullets, thrown stones, etc. are often said to be flying. Seems to me the word gets applied to anything that is moving while not contacting the ground. Yep, like a Ferrari on a motorway -- Tony van der Hoff| mailto:t...@vanderhoff.org Buckinghamshire, England | -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
Chris Angelico wrote: Maybe what the ISS does isn't flying - it's falling with style? Yep. They didn't really launch it into orbit with rockets, that was all faked. They actually hauled it up there with a crane, let it go and pulled the earth away at the last moment. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
Dennis Lee Bieber wrote: I'd say satellites do "not" fly, as they have no force/action opposing the fall caused by the pull of gravity. Arrows, bullets, thrown stones, etc. are often said to be flying. Seems to me the word gets applied to anything that is moving while not contacting the ground. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Fri, Mar 17, 2017 at 1:52 PM, Ian Kellywrote: > On Thu, Mar 16, 2017 at 7:56 PM, Dennis Lee Bieber > wrote: >> On Fri, 17 Mar 2017 10:03:04 +1300, Gregory Ewing >> declaimed the following: >> >>>Chris Angelico wrote: I just asked my father, and he believes that a satellite DOES fly, on the basis that it is perpetually falling and forever missing the ground. >>> >>>Also, NASA people talk about "flying" the ISS, so it seems your >>>father is in good company. >> >> Most likely they are referring to using gyroscopes (reaction wheels), >> and maneuvering thrusters to maintain orientation relative to the sun, so >> the solar panels stay fully lit. > > The ISS orbit is low enough that they also have to do regular > (approximately monthly) boosts to counteract the gradual decay. Maybe what the ISS does isn't flying - it's falling with style? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Thu, Mar 16, 2017 at 7:56 PM, Dennis Lee Bieberwrote: > On Fri, 17 Mar 2017 10:03:04 +1300, Gregory Ewing > declaimed the following: > >>Chris Angelico wrote: >>> I just asked my father, and he believes that a satellite DOES fly, on >>> the basis that it is perpetually falling and forever missing the >>> ground. >> >>Also, NASA people talk about "flying" the ISS, so it seems your >>father is in good company. > > Most likely they are referring to using gyroscopes (reaction wheels), > and maneuvering thrusters to maintain orientation relative to the sun, so > the solar panels stay fully lit. The ISS orbit is low enough that they also have to do regular (approximately monthly) boosts to counteract the gradual decay. -- https://mail.python.org/mailman/listinfo/python-list
RE: Dynamically replacing an objects __class__; is it safe?
Steve D'Aprano wrote,on March 16, 2017 5:07 AM > > On Thu, 16 Mar 2017 09:03 am, Gregory Ewing wrote: > > > Steve D'Aprano wrote: > >> You probably can't make a whale fly just by changing the class to > >> bird. It will need wings, and feathers, at the very least. > > > > Some things succeed in flying with neither wings nor feathers. > > Helicopters, for example. > > For some definition of "no wings". > > But regardless of rockets, balloons, helicopters, Boeing 747s > and Dr Strange's mystical Cloak Of Levitation, *birds* > require wings and feathers to fly. If you just paint "BIRD" > on the side of a whale, it won't get off the ground, and if > you do manage to get it airborne (via a catapult, > perhaps) it will just come down with a rather large splat. > > The point is that dynamically swapping the class of an > existing instance at runtime is *not* just a way of doing > duck-typing. It really does matter if your duck flies by > flapping feathered wings or by blasting an exhaust of hot > gasses out of its rear end at high speed. > > With duck-typing, you don't care whether you have a duck or a > goose, so long as they both can fly: you don't care *how* it > flies, so long as it does, and even a rocket-propelled > balloon will be fine. > > But when you dynamically swap out __class__ (I think > Objective-C "swizzling" is conceptually equivalent, so I'll > call it by that word) you have to care about the > implementation. The whole point of swizzling is that it > allows you to swap out one implementation ("run forward and > flap your wings") with another implementation ("light the > blue touch paper"). But if you do that, you have to care > about implementation details. There's no point in setting > your swizzled fly() method to that of Rocket if your instance > doesn't have blue touch paper to light. > > With duck-typing, making the methods work isn't your > responsibility. But when you swizzle, you are responsible for > making sure that the instance provides whatever the methods > need to work. > > A very old but good example of Python swizzling is here: > http://code.activestate.com/recipes/68429-ring-buffer/ -- Steve "Cheer up," they said, "things could be worse." So I cheered up, and sure enough, things got worse. Condolences, Steve. Nobody on this thread wants to plumb the depths of Python swizzling. Or anything else Python, so it appears. -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
Chris Angelico wrote: I just asked my father, and he believes that a satellite DOES fly, on the basis that it is perpetually falling and forever missing the ground. Also, NASA people talk about "flying" the ISS, so it seems your father is in good company. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Thu, 16 Mar 2017 14:04:19 +, Jon Ribbens wrote: > On 2017-03-16, Robin Beckerwrote: >> On 15/03/2017 13:53, Steve D'Aprano wrote: >>> You probably can't make a whale fly just by changing the class to >>> bird. It will need wings, and feathers, at the very least. >> >> the whale in the Hitchhiker's Guide found itself flying without >> feathers or wings > > Well, only if you redefine "flying" to include "accelerating to terminal > velocity straight downwards under the force of gravity"... Arthur himself was a regular flyer. -- genius, n.: Person clever enough to be born in the right place at the right time of the right sex and to follow up this advantage by saying all the right things to all the right people. -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
Robin Beckerwrote: > On 16/03/2017 14:03, D'Arcy Cain wrote: > > On 2017-03-16 09:45 AM, Robin Becker wrote: > >> On 15/03/2017 13:53, Steve D'Aprano wrote: > >>> You probably can't make a whale fly just by changing the class to > >>> bird. It > >>> will need wings, and feathers, at the very least. > >> > >> the whale in the Hitchhiker's Guide found itself flying without feathers > >> or wings > > > > Falling is not the same as flying unless you accidentally miss the ground. > > > well in English an arrow flies as well as time, the whale was very interested > in > the approaching ground and perhaps forgot to conjure up virtual wings or > rockets. In any case satellites miss the ground, but even their motion cannot > be > classed as flying with wings so I think this discussion has become irrelevant. Time flies like an arrow Fruit flies like a banana -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Fri, Mar 17, 2017 at 1:21 AM, Robin Beckerwrote: >> Falling is not the same as flying unless you accidentally miss the ground. >> > well in English an arrow flies as well as time, the whale was very > interested in the approaching ground and perhaps forgot to conjure up > virtual wings or rockets. In any case satellites miss the ground, but even > their motion cannot be classed as flying with wings so I think this > discussion has become irrelevant. I just asked my father, and he believes that a satellite DOES fly, on the basis that it is perpetually falling and forever missing the ground. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On 16/03/2017 14:03, D'Arcy Cain wrote: On 2017-03-16 09:45 AM, Robin Becker wrote: On 15/03/2017 13:53, Steve D'Aprano wrote: You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. the whale in the Hitchhiker's Guide found itself flying without feathers or wings Falling is not the same as flying unless you accidentally miss the ground. well in English an arrow flies as well as time, the whale was very interested in the approaching ground and perhaps forgot to conjure up virtual wings or rockets. In any case satellites miss the ground, but even their motion cannot be classed as flying with wings so I think this discussion has become irrelevant. -- Robin Becker -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On 16/03/17 13:45, Robin Becker wrote: On 15/03/2017 13:53, Steve D'Aprano wrote: You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. the whale in the Hitchhiker's Guide found itself flying without feathers or wings It didn't so much fly, as plummet. -- Tony van der Hoff| mailto:t...@vanderhoff.org Buckinghamshire, England | -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On 2017-03-16, Robin Beckerwrote: > On 15/03/2017 13:53, Steve D'Aprano wrote: >> You probably can't make a whale fly just by changing the class to bird. It >> will need wings, and feathers, at the very least. > > the whale in the Hitchhiker's Guide found itself flying without > feathers or wings Well, only if you redefine "flying" to include "accelerating to terminal velocity straight downwards under the force of gravity"... -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On 2017-03-16 09:45 AM, Robin Becker wrote: On 15/03/2017 13:53, Steve D'Aprano wrote: You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. the whale in the Hitchhiker's Guide found itself flying without feathers or wings Falling is not the same as flying unless you accidentally miss the ground. -- D'Arcy J.M. Cain Vybe Networks Inc. http://www.VybeNetworks.com/ IM:da...@vex.net VoIP: sip:da...@vybenetworks.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On 15/03/2017 13:53, Steve D'Aprano wrote: You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. the whale in the Hitchhiker's Guide found itself flying without feathers or wings -- Robin Becker -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Thu, 16 Mar 2017 09:03 am, Gregory Ewing wrote: > Steve D'Aprano wrote: >> You probably can't make a whale fly just by changing the class to bird. >> It will need wings, and feathers, at the very least. > > Some things succeed in flying with neither wings nor feathers. > Helicopters, for example. For some definition of "no wings". But regardless of rockets, balloons, helicopters, Boeing 747s and Dr Strange's mystical Cloak Of Levitation, *birds* require wings and feathers to fly. If you just paint "BIRD" on the side of a whale, it won't get off the ground, and if you do manage to get it airborne (via a catapult, perhaps) it will just come down with a rather large splat. The point is that dynamically swapping the class of an existing instance at runtime is *not* just a way of doing duck-typing. It really does matter if your duck flies by flapping feathered wings or by blasting an exhaust of hot gasses out of its rear end at high speed. With duck-typing, you don't care whether you have a duck or a goose, so long as they both can fly: you don't care *how* it flies, so long as it does, and even a rocket-propelled balloon will be fine. But when you dynamically swap out __class__ (I think Objective-C "swizzling" is conceptually equivalent, so I'll call it by that word) you have to care about the implementation. The whole point of swizzling is that it allows you to swap out one implementation ("run forward and flap your wings") with another implementation ("light the blue touch paper"). But if you do that, you have to care about implementation details. There's no point in setting your swizzled fly() method to that of Rocket if your instance doesn't have blue touch paper to light. With duck-typing, making the methods work isn't your responsibility. But when you swizzle, you are responsible for making sure that the instance provides whatever the methods need to work. A very old but good example of Python swizzling is here: http://code.activestate.com/recipes/68429-ring-buffer/ -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Wed, 15 Mar 2017 19:10:17 -0700, Deborah Swanson wrote: > MRAB wrote, on Wednesday, March 15, 2017 3:19 PM >> >> On 2017-03-15 22:03, Gregory Ewing wrote: >> > Steve D'Aprano wrote: >> >> You probably can't make a whale fly just by changing the class to >> >> bird. It will need wings, and feathers, at the very least. >> > >> > Some things succeed in flying with neither wings nor feathers. >> > Helicopters, for example. >> > >> Could you argue that the blades were a kind of wing? After all, they >> rely on the same principle of moving through the air to produce lift. >> >> Balloons, on the other hand, ... :-) > > or kites... ;) no kites are a wing, although they are dependant on the string to hold them up. -- Too many people are thinking of security instead of opportunity. They seem more afraid of life than death. -- James F. Byrnes -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Thu, 16 Mar 2017 11:03:56 +1300, Gregory Ewing wrote: > Steve D'Aprano wrote: >> You probably can't make a whale fly just by changing the class to bird. >> It will need wings, and feathers, at the very least. > > Some things succeed in flying with neither wings nor feathers. > Helicopters, for example. helicopters DO have wings. that is why they are technical referred to as ROTARY WING Aircraft ;-) -- Boy, that crayon sure did hurt! -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
MRAB wrote: Could you argue that the blades were a kind of wing? After all, they rely on the same principle of moving through the air to produce lift. Balloons, on the other hand, ... :-) Also rockets. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
RE: Dynamically replacing an objects __class__; is it safe?
MRAB wrote, on Wednesday, March 15, 2017 3:19 PM > > On 2017-03-15 22:03, Gregory Ewing wrote: > > Steve D'Aprano wrote: > >> You probably can't make a whale fly just by changing the class to > >> bird. It will need wings, and feathers, at the very least. > > > > Some things succeed in flying with neither wings nor feathers. > > Helicopters, for example. > > > Could you argue that the blades were a kind of wing? After all, they > rely on the same principle of moving through the air to produce lift. > > Balloons, on the other hand, ... :-) or kites... ;) -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Thu, Mar 16, 2017 at 9:19 AM, MRABwrote: > On 2017-03-15 22:03, Gregory Ewing wrote: >> >> Steve D'Aprano wrote: >>> >>> You probably can't make a whale fly just by changing the class to bird. >>> It >>> will need wings, and feathers, at the very least. >> >> >> Some things succeed in flying with neither wings nor feathers. >> Helicopters, for example. >> > Could you argue that the blades were a kind of wing? After all, they rely on > the same principle of moving through the air to produce lift. > > Balloons, on the other hand, ... :-) Certainly not. If they were, you'd move (or spin) them faster to go up, and slower to go down. But you don't. So it's all about the ugliness. You twist the blades up a bit to make the helicopter uglier. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On 2017-03-15 22:03, Gregory Ewing wrote: Steve D'Aprano wrote: You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. Some things succeed in flying with neither wings nor feathers. Helicopters, for example. Could you argue that the blades were a kind of wing? After all, they rely on the same principle of moving through the air to produce lift. Balloons, on the other hand, ... :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Thu, Mar 16, 2017 at 9:03 AM, Gregory Ewingwrote: > Steve D'Aprano wrote: >> >> You probably can't make a whale fly just by changing the class to bird. It >> will need wings, and feathers, at the very least. > > > Some things succeed in flying with neither wings nor feathers. > Helicopters, for example. Yeah but they do it by being so ugly that the earth repels them. I'm not sure that whale.ugliness is valid or high enough. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
Steve D'Aprano wrote: You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. Some things succeed in flying with neither wings nor feathers. Helicopters, for example. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
Hi, You can subclass M2 from M1 and override only what you need. Regards, -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Wed, 15 Mar 2017 08:54 pm, marco.naw...@colosso.nl wrote: > Dear All, > > Summary of the question: > Is it generally safe to dynamically change an objects class; if not > under which conditions can it be considered safe. *Generally* safe? No. You cannot expect to take an arbitrary object and change its class: animal = Whale() animal.__class__ = Bird animal.fly() You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. For pure-Python classes, it is "safe" in the sense that the worst that will happen is an exception, not a seg fault or core dump. But it is not safe in the sense that the instance will work correctly: methods may fail, or do he wrong thing. (Actually, *silently doing the wrong thing* is usually much worse than raising an exception or crashing. At least when the program crashes, you know it is broken!) For *cooperative* classes, where you design the Before and After classes to operate correctly when you change the __class__, that's perfectly safe. > Context: > Given the code below, I have no direct control over Base and M1. M1 > is a instantiated by 'calling' the read-only property of Base. > I would like to transparently switch behaviour from M1 to M2. I have > tried several things, but finally settled on the code below. Although > it works really well, I am not sure whether I am about to shoot myself > in the foot. In short, what I do is derive from Base, shadow the read-only > property 'my_prop' so it returns M2 and finally replace an objects > __class__ attribute with my derived class. Sounds scarily confusing, and I say that as somebody who LOVES the "swap the __class__" trick when it is appropriate. If I wouldn't risk swapping out the __class__ if I didn't control the classes. You have this: o = Base() o.my_prop # Prints 'Initializing M1' o = Base() o.__class__ = ShadowBase o.my_prop # Prints 'Initializing M2' But why not just do this? o = ShadowBase() # inherits from Base, overrides my_prop o.my_prop # Prints 'Initializing M2' This sounds like an ordinary example of class inheritance to me, no need to add complications with swapping out the __class__ for something different. The only reason I would consider using the __class__ trick here is if you don't control the instantiation of the object: you don't create your own instance, you receive it pre-instantiated from elsewhere. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Wednesday, March 15, 2017 at 11:33:56 AM UTC+1, Peter Otten wrote: > marco.naw...@colosso.nl wrote: > > > Dear All, > > > > Summary of the question: > > Is it generally safe to dynamically change an objects class; if not > > under which conditions can it be considered safe. > > > > Context: > > Given the code below, I have no direct control over Base and M1. M1 > > is a instantiated by 'calling' the read-only property of Base. > > I would like to transparently switch behaviour from M1 to M2. I have > > tried several things, but finally settled on the code below. Although > > it works really well, I am not sure whether I am about to shoot myself > > in the foot. In short, what I do is derive from Base, shadow the read-only > > property 'my_prop' so it returns M2 and finally replace an objects > > __class__ attribute with my derived class. > > > > Any thoughts or comments? > > Saying that something is "safe" is always problematic. I'd rather look for > ways to break the code and then decide if you can live with these potential > problems. I'll start with > > - If Base is reimplemented in C __class__ may become read-only > - my_prop may be accessed by Base methods and expect an M1 instance Ok. These are valid points. The first is not a real concern to me at the moment. The whole purpose of Base is to provide a higher level interface to the underlying C/C++ code. The second is more interesting. I was aware of this and checked the source code of the library. I know an M1 instance is not directly used by Base. In addition, I expose the exact same interface in M2. Thanks for the feedback. Much appreciated! Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
marco.naw...@colosso.nl wrote: > Dear All, > > Summary of the question: > Is it generally safe to dynamically change an objects class; if not > under which conditions can it be considered safe. > > Context: > Given the code below, I have no direct control over Base and M1. M1 > is a instantiated by 'calling' the read-only property of Base. > I would like to transparently switch behaviour from M1 to M2. I have > tried several things, but finally settled on the code below. Although > it works really well, I am not sure whether I am about to shoot myself > in the foot. In short, what I do is derive from Base, shadow the read-only > property 'my_prop' so it returns M2 and finally replace an objects > __class__ attribute with my derived class. > > Any thoughts or comments? Saying that something is "safe" is always problematic. I'd rather look for ways to break the code and then decide if you can live with these potential problems. I'll start with - If Base is reimplemented in C __class__ may become read-only - my_prop may be accessed by Base methods and expect an M1 instance > (code below is Python 3) > > class M1(object): > > def __init__(self): > print('Initializing M1') > > > class M2(object): > > > def __init__(self): > print('Initializing M2') > > > > > class Base(object): > > > @property > def my_prop(self): > return M1() > > > class ShadowBase(Base): > > @property > def my_prop(self): > return M2() > > if __name__ == '__main__': > > o = Base() > o.my_prop > # Prints 'Initializing M1' > > o = Base() > o.__class__ = ShadowBase > o.my_prop > # Prints 'Initializing M2' -- https://mail.python.org/mailman/listinfo/python-list
Dynamically replacing an objects __class__; is it safe?
Dear All, Summary of the question: Is it generally safe to dynamically change an objects class; if not under which conditions can it be considered safe. Context: Given the code below, I have no direct control over Base and M1. M1 is a instantiated by 'calling' the read-only property of Base. I would like to transparently switch behaviour from M1 to M2. I have tried several things, but finally settled on the code below. Although it works really well, I am not sure whether I am about to shoot myself in the foot. In short, what I do is derive from Base, shadow the read-only property 'my_prop' so it returns M2 and finally replace an objects __class__ attribute with my derived class. Any thoughts or comments? (code below is Python 3) class M1(object): def __init__(self): print('Initializing M1') class M2(object): def __init__(self): print('Initializing M2') class Base(object): @property def my_prop(self): return M1() class ShadowBase(Base): @property def my_prop(self): return M2() if __name__ == '__main__': o = Base() o.my_prop # Prints 'Initializing M1' o = Base() o.__class__ = ShadowBase o.my_prop # Prints 'Initializing M2' -- https://mail.python.org/mailman/listinfo/python-list