Hmm, I think your "object" class suffers from the same problem as your "properties" attribute: it's like you're reimplementing Python within Python.
[*]Your architecture idea is to have a very generic base object, with a 'properties' attribute.
[*]Then you create more specialized objects by adding properties to ' properties'.
[*]Some objects have to be similar with the same properties, so you have a global object that stores a starter set of properties, with default values
[*] And finally you have functions (like createThing) that make instances of those special objects
But if you think about it, all those things are already part of the architecture of Python itself.
[*]Your generic base object is just a normal Python object
[*] You make Python objects specilaized by giving them special properties. They are just called attributes in Python.
[*] A "class" is just a global thing that defines "default" properties for a specialized kind of object, so you can easily make for than one. Just like your BASE_CELL, except better because you define methods in the same place
[*] And classes have the __init__ method that creates instances.
See what I mean? The people who invented OOP ran into the same problems as you, and invented similar trickses to make it work. The trickses are already in the language, try to use them
Compare this:
- Code: Select all
BASE_CELL = {
'floor' : True,
'color' : libtcod.grey,
'inventory' : []
}
class Object():
pass
class Cell(Object):
def __init__ (self, properties = {}, container = []):
self.properties = properties
self.container = container
def draw(self, xyz):
//stuff
def clear(self, xyz):
//stuff
def createCell(prop = {}, cont = []):
return Cell(prop, cont)
myCell = createCell(BASE_CELL)
myCell.properties['hitPoints'] = 15
With this:
- Code: Select all
class Cell():
def __init__ (self):
self.floor = true
self.color = libtcod.grey
self.inventory = []
self.container = [] // what does the container even do?
def draw(self, xyz):
//stuff
def clear(self, xyz):
//stuff
myCell = Cell()
mycell.hitPoints = 15
See what I am getting at? The normal structure of Python is already a near-perfect fit for your architecture, because it is based on the same ideas.
As you can see, you can even add attrbutes on the fly. You will find that this flexibility can be nice, but also dangerous. That's true for the attributes, but also for your properties. The classes help you to put a predictable structure in your code, and you are going to need every bit of structure you can find. Be flexible, but not more than necessary!
Partially, it is a naming issue. But naming is important, it's how you tell yourself what struture you are trying to make.
If you have a base class called Object, do you also expect other classes that aren't Objects? If so, what's special about classes that inherit from Object? Use that difference to give it a better name. If you cannot think of a simple name that describes how they are different from other objects, you probably should think longer about it really is supposed to do and don't.
Same for properties: what things might an object have that are
not properties? How are properties different? If everything is a property, then why have a special category for it? If properties are special, different from other attributes of the class, then how are they special? Again, if you can't come up with a simple and clear description of the difference, your own mental image of the category is probably not yet sharp enough.