Stop overriding Model.__init__()
in user models, use a setup
method instead
#2354
Replies: 2 comments 4 replies
-
A couple of thoughts
In short, I don't agree with the arguments given here, but I am not in principle against exploring this further |
Beta Was this translation helpful? Give feedback.
-
Thanks for bringing this up! I think this goes quite well into another idea I have had for quite some time in the back of my head. My idea was to go done a decorator path, similar to a import mesa
@mesa.Model
class MyModel:
pass But building a prototype around this I still got some headaches merging the @mesa.Model(
seed=123,
space=Orthogonal2DGrid() # automatically add space attribute
....)
class MyModel:
def setup(foo, bar):
pass
model1 = MyModel(1foo, 2bar, seed=456) # <- We can program the decorator to automatically pass args to setup and have some kwargs passed to __init__ In the last line you can see that this approach could also work with The idea of a decorator approach came back recently to me especially for agents, where for example providing a space would automatically add the appropriate movement methods and things like that. |
Beta Was this translation helpful? Give feedback.
-
Context and Motivation
In the current Mesa framework, users are advised to override the
Model.__init__
method to set up their custom models. However, this approach has led to some challenges:super().__init__()
in their custom__init__
method.*args
and**kwargs
throughsuper().__init__()
can lead to confusing and "ugly" code.Model.__init__
signature in future versions of Mesa could potentially break user-defined models.To address these issues and provide a cleaner, more flexible approach to model initialization, we propose introducing a new
setup
method.Proposed Changes
setup
method in theModel
class:setup
instead of__init__
in their custom models:Benefits
super().__init__()
or handling*args
and**kwargs
.Model.__init__
in the future without breaking user models.setup
method.super().__init__()
.Potential Drawbacks
Beta Was this translation helpful? Give feedback.
All reactions