了解班级的概念

时间:2018-10-25 11:32:11

标签: python python-3.x function class dictionary

所以对于你们大多数人来说,这可能是一件非常简单的事情,但是我才刚开始。

以下是我第一次上课:

class WeatherYear:

    DEFAULT_MAX = -99
    DEFAULT_MIN = 999
    DEFAULT_STR = 'DATE'

    def __init__(self, year):
        self.year = year
        self.max_temp = WeatherYear.DEFAULT_MAX
        self.min_temp = WeatherYear.DEFAULT_MIN
        self.max_temp_date = WeatherYear.DEFAULT_STR
        self.max_humid = WeatherYear.DEFAULT_MAX
        self.min_humid = WeatherYear.DEFAULT_MIN

    def add_day(self, date, max_temp, min_temp, max_humid, min_humid):
        if max_temp and max_temp > self.max_temp:
            self.max_temp = max_temp
            self.max_temp_date = date
        if min_temp and min_temp < self.min_temp:
            self.min_temp = min_temp
        if max_humid and max_humid > self.max_humid:
            self.max_humid = max_humid
        if min_humid and min_humid < self.min_humid:
            self.min_humid = min_humid

这是我用于其中的一些东西:

years_dict = {}
wy_obj = years_dict.get(year)

if not wy_obj:
    years_dict[year] = WeatherYear(year)
    wy_obj = years_dict[year]

    wy_obj.add_day(date, max_temp, min_temp, max_humid, min_humid)

这是我代码的一部分,有人可以向我解释确切发生了什么,还是主要部分,我是否需要在__init__函数中保留年份?如果我删除了该怎么办?仍然会以相同的方式工作吗?

2 个答案:

答案 0 :(得分:0)

您应该尝试一下代码,这是理解Python中OOP概念的最佳方法。 回到您的问题,根据您的代码,您需要在 init 函数中添加 year ,否则self.year = year会给出未定义 year的错误

答案 1 :(得分:0)

  

问题> :我是否需要在 init 函数中保留年份?

,因为您将日期存储在self.max_temp_date中,因此可以翻倍,因为您可以使用year从日期中获取self.max_temp_date.year


将您的class WetherYear().__init__(...更改为以下内容:

def __init__(self, measure):
    self.max_temp_date, self.max_temp, self.min_temp, self.max_humid, self.min_humid = measure

因此,您不必使用哑数值来初始化类属性。使用前measure个值一次设置属性。

关于您对class methodes的命名,将其命名为正在执行的功能。在这种情况下,您不会根据条件添加任何您更新的内容。

class attributs

您不必def add_day(self, date, max_temp, min_temp, max_humid, min_humid): if max_temp and max_temp > self.max_temp: ... 对象get(...来测试此WeatherYear是否存在。仅使用键year。同样,您也不必year再次获得它,可以从wy_obj = years_dict[year]中使用它,例如dict

years_dict[year].add_day(...

考虑以下示例:

wy_obj = years_dict.get(year)

if not wy_obj:
    years_dict[year] = WeatherYear(year)
    wy_obj = years_dict[year]
    wy_obj.add_day(...
  

输出

from datetime import date

class WeatherYear:
    def __init__(self, measure):
        self.max_temp_date, self.max_temp, self.min_temp, self.max_humid, self.min_humid = measure

    def update(self, measure):            
        if measure[1] > self.max_temp:
            self.max_temp_date = measure[0]
            self.max_temp = measure[1]

        if measure[2] > self.min_temp:
            self.min_temp = measure[2]

        if measure[3] > self.max_humid:
            self.max_humid = measure[3]

        if measure[4] > self.min_humid:
            self.min_humid = measure[4]

    def __str__(self):
       return "WeatherYear:In {year} at {self.max_temp_date}, measured a max temperature of {self.max_temp}°C and max humid of {self.max_humid} %".format(self=self, year=self.max_temp_date.year)


def main():
    #          date, max_temp, min_temp, max_humid, min_humid
    measurements = [(date(2018, 9, 10), 20, 8, 40, 30),
                    (date(2018, 9, 12), 25, 12, 50, 35),
                    (date(2018, 9, 13), 19, 10, 30, 25)]

    max_years = {}
    for measure in measurements:
        if not measure[0].year in max_years:
            max_years[measure[0].year] = WeatherYear(measure)
        else:
            max_years[measure[0].year].update(measure)

    for year in max_years:
        print("{}: {}".format(year, max_years[year]))


if __name__ == "__main__":
    main()

使用Python测试:3.4.2

相关问题