Bill Armstrong
Bill Armstrong

Reputation: 1777

Rerunning __init__ method

Assuming you have a class defined as follows:

class Bar():
     def __init__(self):
          #do something on initialization

and then create a new object:

foo = Bar()

I understand that the creation of foo executes the __init__ method in the class.

If, however, later I would like to re-run the #do something else on initialization code - how is that done? My foo object has other attributes that are specific to the foo instance.

If the answer is create a new object, such as foo2 = Bar() a second time, then is there a way to preserve the original foo object attributes that were created and associate them with foo2?

Being new to Python, I'm trying not to rewrite the __init__ code again in a second method.

Upvotes: 1

Views: 1900

Answers (1)

eddiewould
eddiewould

Reputation: 1633

Why not just put your initialisation code in a method and call it from init as well as the other locations you need it called from?

Upvotes: 1

Related Questions