On Thu, 12 Mar 2015 16:31:12 +1100, Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> wrote:
>Mario Figueiredo wrote: > > >If this is supposed to be a singleton, you can't create more instances. The >point of a singleton that there is only one instance (or perhaps a small >number, two or three say). Why do you need two different ways to create >instances if you only have one instance? > The map is instantiated from the contents of a saved map file on the main application. A different application, a map editor, needs to also instantiate an object of the class Map. But in this case the map needs to either be empty (if the user wants to create a new map), or loaded from the saved map file (if the user wants to edit an existing map). > >> I added the following method to the class definition, above: >> >> @classmethod >> def generate(cls, width, height, fill=terrain[6]): >> if Map._instance is None: >> Map._instance = super(Map, cls).__new__(cls) >> else: >> raise Exception('Cannot generate an instance of Map.') >> >> Map._instance.author = None >> Map._instance.name = None > >Since this method modifies the singleton instance in place, it doesn't >generate a new instance. It shouldn't be called generate(). No sure what you mean. That method either creates a new instance or raises an exception. It doesn't modify an instance in-place. > >> Map._instance.description = None >> # etc... >> self.cells = [Cell(fill)] * width * height >> return Map._instance > >That's not your actual code, since the indentation is wrong. Map._instance.description = None # etc... self.cells = [Cell(fill)] * width * height return Map._instance -- https://mail.python.org/mailman/listinfo/python-list