我有一个主班叫预报。我具有__init__函数,并创建了诸如self.temp_c和self.city之类的变量。在该类中,我做了一个名为“当前”的子类。但是,我希望“当前”子类具有访问权限 从__init__函数转到self.city。因此,在用户初始化变量(城市,日期,天数)之后,他就可以获取有关该城市的信息。
我尝试使用super().__ init __(city),但不起作用。并且还尝试使当前类单独成为一个类并从预测类继承。
这是我的代码:
class forecast:
def __init__(self, city, day=0, days=5):
''' days is the amount of days you want ot forecast for and day is the
day you want to view the forecast for. 0 would be first and last would
be the amount set for days. Leave out day for first day '''
self.city = city
self.full_json = client.getForecastWeather(q=city, days=days)
self.api_url = 'http://api.apixu.com/v1/'
self.forecast_json = self.full_json['forecast']['forecastday']
self.text = self.forecast_json[day]['day']['condition']['text']
self.date = self.forecast_json[day]['date']
self.max_c = self.forecast_json[day]['day']['maxtemp_c']
self.max_f = self.forecast_json[day]['day']['maxtemp_f']
self.avg_c = self.forecast_json[day]['day']['avgtemp_c']
self.avg_f = self.forecast_json[day]['day']['avgtemp_f']
self.avg_hum = self.forecast_json[day]['day']['avghumidity']
self.min_c = self.forecast_json[day]['day']['mintemp_c']
self.min_f = self.forecast_json[day]['day']['mintemp_f']
self.astro_info = self.forecast_json[day]['astro']
self.sunrise = self.astro_info['sunrise']
self.sunset = self.astro_info['sunset']
self.moonrise = self.astro_info['moonrise']
self.moonset = self.astro_info['moonset']
self.uv = self.forecast_json[day]['day']['uv']
class current:
#Hoping for:
self.full_json = client.getCurrentWeather(q=forecast.self.city)# Or however I access that.
self.lat = full_json['location']['lat']
self.lon = full_json['location']['lon']
self.text = full_json['current']['condition']['text']
self.temp_c = full_json['current']['temp_c']
self.temp_f = full_json['current']['temp_f']
或者我可以:
class forecast:
def __init__(self, city, day=0, days=5):
''' days is the amount of days you want ot forecast for and day is the
day you want to view the forecast for. 0 would be first and last would
be the amount set for days. Leave out day for first day '''
self.city = city
self.full_json = client.getForecastWeather(q=city, days=days)
self.api_url = 'http://api.apixu.com/v1/'
self.forecast_json = self.full_json['forecast']['forecastday']
self.text = self.forecast_json[day]['day']['condition']['text']
self.date = self.forecast_json[day]['date']
self.max_c = self.forecast_json[day]['day']['maxtemp_c']
self.max_f = self.forecast_json[day]['day']['maxtemp_f']
self.avg_c = self.forecast_json[day]['day']['avgtemp_c']
self.avg_f = self.forecast_json[day]['day']['avgtemp_f']
self.avg_hum = self.forecast_json[day]['day']['avghumidity']
self.min_c = self.forecast_json[day]['day']['mintemp_c']
self.min_f = self.forecast_json[day]['day']['mintemp_f']
self.astro_info = self.forecast_json[day]['astro']
self.sunrise = self.astro_info['sunrise']
self.sunset = self.astro_info['sunset']
self.moonrise = self.astro_info['moonrise']
self.moonset = self.astro_info['moonset']
self.uv = self.forecast_json[day]['day']['uv']
class current:
def __init__(self, city):
self.full_json = client.getCurrentWeather(q=city)
self.lat = full_json['location']['lat']
self.lon = full_json['location']['lon']
self.text = full_json['current']['condition']['text']
self.temp_c = full_json['current']['temp_c']
self.temp_f = full_json['current']['temp_f']