我需要在构造函数中返回一个Bike实例。例如:从构造函数返回一个类的实例
class Bike(object):
def __init__(self,color):
self.bikeColor = color
return self #It should return an instance of the class; is this not right?
myBike = Bike("blue")
当我做以上,我得到以下错误:
TypeError: __init__() should return None, not 'Bike'
如果是这样的话,我怎么会返回一个实例,如果它只是猜想return None
?
不,它应该返回Non e如错误所示 – JBernardo
这是一个初始值设定项。如果你想返回实例,请尝试'__new__' – JBernardo