I want to have a class named ProjectDirectory and a class named MetaDirectory. Each project has a MetaDirectory which contains some meta data. Is it the good way to write the classes like this:
class ProjectDirectory(object):
def __init__(self, directory=None):
self.directory = directory
self.meta_directory = MetaDirectory(self)
def __repr__(self):
return self.directory
class MetaDirectory(object):
def __init__(self, project_directory=None):
self.project_directory = project_directory
self.directory = "%s/.meta/" % project_directory
ProjectDirectory has a reference to MetaDirectory and MetaDirectory has a reference to ProjectDirectory.
Is there an other solution or this solution is good ?
That’s perfectly fine, but since there is now a one to one connection between the classes, you could actually merge them. If you have many types of Directories beside ProjectDirectory, you could inherit from MetaDirectory instead.
But if for some reason you don’t want to, the design above is fine. There isn’t anything wrong with it per se.