Creating a static class with no instances

后端 未结 4 1161
长发绾君心
长发绾君心 2021-02-02 05:22

All of the tutorials I see online show how to create classes with __init__ constructor methods so one can declare objects of that type, or instances of that class.<

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 05:55

    Seems that you need classmethod:

    class World(object):
    
        allAirports = []
    
        @classmethod
        def initialize(cls):
    
            if not cls.allAirports:
                f = open(os.path.expanduser("~/Desktop/1000airports.csv"))
                file_reader = csv.reader(f)
    
                for col in file_reader:
                    cls.allAirports.append(Airport(col[0],col[2],col[3]))
    
            return cls.allAirports
    

提交回复
热议问题