I’m writing a Python script to parse some data. At the moment I’m trying to make a Class which creates “placeholder” objects. I intend to repeatedly pass each “placeholder” variables, and finally turn it into a dict, float, list or string. For reasons that would take a while to describe, it would be a lot easier if I could replace the instance by calling a method.
Here’s a simplified example
class Event( dict ):
def __init__( self ):
self.sumA = 0.0
self.sumB = 0.0
def augmentA( self, i ):
self.sumA += i
def augmentB( self, i ):
self.sumB += i
def seal( self ):
if self.sumA != 0 and self.sumB != 0:
self = [ sumA, sumB ]
elif self.sumA != 0:
self = float( sumA )
elif self.sumB != 0:
self = float( sumB )
And what I want to do is:
e = Event()
e.augmentA( 1 )
e.augmentA( 2 )
e.seal()
…and have ‘e’ turn into a float.
What I am hoping to avoid is:
e = Event()
e.augmentA( 1 )
e.augmentA( 2 )
e = e.getSealedValue()
I totally understand that “self” in my “seal” method is just a local variable, and won’t have any effect on the instance outside scope. I’m unsure however how to achieve what I want, from within the instance, where it would be most convenient for my code. I also understand I could override all the bulit-ins ( getItem, toStr ) but that complicates my code a lot.
I’m a Python noob so I’m unsure if this is even possible. Indulge me, please 🙂
Under some circunstances, Python allows you to change the class of an object on the fly. However, not any object can be converted to any class, as the example below demonstrates (newlines added for readability):
(I don’t know the exact rules from the top of my head, but if your classes uses
__slots__for instance they must be compatible for the conversion to be possible)However, as other answerers pointed out, in general it’s a very bad idea to do so, even if there was a way to convert every reference to one object to a reference to another. I wouldn’t go as far as saying never do that though, there might be legitimate uses of this technique (for instance, I see it as an easy way of implementing the State Design Pattern without creating unnecessary clutter).