Repository navigation
feat: add __str__ to Agent base class - #3652
Conversation
|
The failing check (test_pd_grid_model[chromium]) is a pre-existing flaky Playwright browser test unrelated to this change. All 611 other tests pass. |
|
Performance benchmarks:
|
|
|
||
| def __str__(self) -> str: | ||
| """Return a string representation of the agent.""" | ||
| return f"<{self.__class__.__name__} ID:{self.unique_id}>" |
There was a problem hiding this comment.
Fixed — removed the angle brackets. The format is now aligned with @quaquel's suggestion: f"{self.class.name}, agent_id = {self.unique_id}"
| def __repr__(self) -> str: | ||
| """Return a detailed string representation of the agent.""" | ||
| return ( | ||
| f"{self.__class__.__name__}(unique_id={self.unique_id}, model={self.model})" |
There was a problem hiding this comment.
I'm not sure what the maintainers want out of __repr__
Right now, eval() will throw error with this implementation.
In my opinion, since there is unique_id, It's okay to have __repr__ this way and not worry about recreation from __repr__
There was a problem hiding this comment.
Agreed — repr has been removed from this PR as it's out of scope. That can be addressed separately once maintainers align on the expected behaviour.
73f8a2b to
f0d1eff
Compare
(cherry picked from commit 43d3680)
Pre-PR Checklist
Approval Link
#3625 (comment)
Summary
Adds
__str__to theAgentbase class for human-readable string representation.Motive
Currently, printing an agent requires manually accessing
agent.__class__.__name__andagent.unique_id. With__str__, users can simply useprint(agent)orf"{agent}"in logging and debugging, improving readability across the framework.Implementation
Added a single
__str__method to theAgentclass inmesa/agent.py:No hook pattern — subclasses can override
__str__directly if needed.__repr__is out of scope for this PR.Usage Examples
Additional Notes
Format aligned with @quaquel's suggestion in #3625.
cc @IlamaranMagesh for review.
Closes #3625