The __init__()
function gets called when object is created.
Is it ok to call an object __init__()
function again, after its been created?
You can, but it's kind of breaking what __init__
is intended to do. A lot of Python is really just convention, so you might as well follow then and expect __init__
to only be called once. I'd recommend creating a function called init
or reset
or something which sets the instance variables, use that when you want to reset the instance, and have __init__
just call init
. This definitely looks more sane:
x = Pt(1,2)
x.set(3,4)
x.set(5,10)
It's fine to call __init__
more than once on an object, as long as __init__
is coded with the effect you want to obtain (whatever that may be). A typical case where it happens (so you'd better code __init__
appropriately!-) is when your class's __new__
method returns an instance of the class: that does cause __init__
to be called on the returned instance (for what might be the second, or twentieth, time, if you keep "recycling" instances via your __new__
!-).
As far as I know, it does not cause any problems (edit: as suggested by the kosher usage of super(...).__init__(...)
), but I think having a reset()
method and calling it both in __init__()
and when you need to reset would be cleaner.