One problem with this class is that reset
doesn't result in an A
that "has just been constructed", which will probably surprise people.
If it were either
class A:
def __init__(self, x):
self.x = x
def init(self, y):
self.y = y**2
def reset(self):
del(self.y)
or (more preferably)
class A:
def __init__(self, x):
self.x = x
self.y = 0
def init(self, y):
self.y = y**2
def reset(self):
self.y = 0
then it would be less bad.
That being said, I dislike any class that has a setup
, gather members
, init
or whatever named method, which users must call before the real purpose of the class can be used. If you really need two phase initialisation, return a factory object from the first call, and only instantiate on the second
class A:
def __init__(self, x, y):
self.x = x
self.y = y**2
# members can assume y exists
def defferedA(x):
def inner(y):
return A(x, y)
return inner
x
mandatory andy
optional? Why is the square ofy
stored rather thany
?