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:
passBut 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.
Uh oh!
There was an error while loading. Please reload this page.
-
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.*argsand**kwargsthroughsuper().__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
setupmethod.Proposed Changes
setupmethod in theModelclass:setupinstead of__init__in their custom models:Benefits
super().__init__()or handling*argsand**kwargs.Model.__init__in the future without breaking user models.setupmethod.super().__init__().Potential Drawbacks
Beta Was this translation helpful? Give feedback.
All reactions