I am making a simple Entity Component System in python using dictionaries (I heard they have better performance somehow), but I didn't wanted access the data like foo['bar']['x'], and wanted to access it more like foo.bar.x (just personal preference)
So I made this code
class Entity(dict):
def __init__(self, dictionary={}):
super().__init__(dictionary)
def __getattr__(self, attribute):
return self[attribute]
foo = Entity([('PositionComponent', {'x': 0, 'y': 0})])
So, I can access the PositionComponent as foo.PositionComponent
but I cant access the x value as foo.PositionComponent.x
How could I make it work? And is there a better way to make what I've already made?
UPDATE
I made three versions of the same ECS in python
- Using Entity classes without extending dict
foo.component(bar).x(for loop in foo.component) - Using Entity classes extending dict
foo['bar']['x'] - Using Entity classes extending dict and accessing components with custom __getattr__
foo.bar.x
Using a particle system, with 133 particles on screen at any time, as test I got the FPS of each ECS

This are the results:
- FPS around 750
- FPS around 1050
- FPS around 500
So this proves that using custom __getattr__ makes the code run 2x slower than using normal dicts
Max particles on screen

(left: FPS, Right Particles)
(Color disabled, circle gets drawn even with radius 0)
(12000 to reach 30 FPS)