Possible Duplicate:
What is a clean, pythonic way to have multiple constructors in Python?
难道不能在 Python 中定义多个具有不同签名的构造函数吗?如果没有,绕过它的一般方法是什么?
例如,假设您想定义一个类City
。
我希望能够说 someCity = City()
或 someCity = City("Berlin")
,其中第一个只是给出默认名称值,第二个定义它。
最佳答案
与 Java 不同,您不能定义多个构造函数。但是,如果没有通过,您可以定义一个默认值。
def __init__(self, city="Berlin"):
self.city = city
关于python - 在 Python 中不能定义多个构造函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2164258/