2024-07-05 11:22:12 发布
网友
我有一个特定的类,在设置__init__构造函数时遇到了困难。我需要它接受零个或多个字符串作为参数,每个字符串提供一个城市名称和州缩写,并指示美国城市旅游的目的地。你知道吗
__init__
例如:
Tour("New York, NY", "Lansing, MI", "Los Angeles, CA")
代表从纽约市开始,到兰辛,最后到洛杉矶的旅行。你知道吗
你知道如何使用Python3.3来实现这一点吗?你知道吗
This may help...
示例
class Bar: def __init__ (self, arg1=None, arg2=None, ... argN=None): class NEW (Bar): def __init__ (self, my_new_arg=None, ??? ) self.new_arg = my_new_arg Bar.__init__(self, ??? )
像这样:
class Tour: def __init__(self, *cities): # cities is a tuple of arguments, do what you want with it
你知道吗
当你打电话的时候
__init__中的cities将设置为("New York, NY", "Lansing, MI", "Los Angeles, CA")。你知道吗
cities
("New York, NY", "Lansing, MI", "Los Angeles, CA")
示例
像这样:
你知道吗
当你打电话的时候
__init__
中的cities
将设置为("New York, NY", "Lansing, MI", "Los Angeles, CA")
。你知道吗相关问题 更多 >
编程相关推荐