2

While reloading python modules it's not hard and assure you that new objects will use the new code, the problem comes around existing objects.

Is it possible to upgrade existing objects so they will use the new code? We can assume that we know what classes were changed.

Still, it is possible that some additional functionality was added to __init__(), so just copying their dictionaries it not enough. One possible solution I considered was to assure that these objects can be upgraded by doing something like obj = MyClass(obj), meaning that __init__ should be written in such way that it would not break if called several times.

1 Answer 1

3

The solution you are considering is perfectly reasonable, but I would suggest adding a static method for creating an updated version of the object instead of adding the additional functionality to __init__(). Consider the following example:

class Point(object):
    def __init__(x, y):
        self.x = x
        self.y = y

    @staticmethod
    def init_from_point(obj):
        return Point(obj.x, obj.y)

With this you could do obj = Point.init_from_point(obj) for your update, without complicating your __init__() at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.