Skip to content

feat: add __str__ to Agent base class - #3652

Merged
quaquel merged 5 commits into
mesa:mainfrom
Atharva76-cpu:fix/add-str-to-agent
Apr 4, 2026
Merged

quaquel merged 5 commits into
mesa:mainfrom
Atharva76-cpu:fix/add-str-to-agent

Conversation

@Atharva76-cpu

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • I linked an issue/discussion where a maintainer approved this enhancement/feature for implementation.

Approval Link

#3625 (comment)

Summary

Adds __str__ to the Agent base class for human-readable string representation.

Motive

Currently, printing an agent requires manually accessing agent.__class__.__name__ and agent.unique_id. With __str__, users can simply use print(agent) or f"{agent}" in logging and debugging, improving readability across the framework.

Implementation

Added a single __str__ method to the Agent class in mesa/agent.py:

def __str__(self) -> str:
    return f"{self.__class__.__name__}, agent_id = {self.unique_id}"

No hook pattern — subclasses can override __str__ directly if needed. __repr__ is out of scope for this PR.

Usage Examples

# Before
print(f"{agent.__class__.__name__}:{agent.unique_id} is called")
# Output: SchellingAgent:1 is called

# After
print(f"{agent} is called")
# Output: SchellingAgent, agent_id = 1 is called

Additional Notes

Format aligned with @quaquel's suggestion in #3625.
cc @IlamaranMagesh for review.
Closes #3625

@Atharva76-cpu

Copy link
Copy Markdown
Contributor Author

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.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.5% [-1.1%, +0.0%] 🔵 -0.8% [-1.1%, -0.5%]
BoltzmannWealth large 🔵 -0.3% [-1.0%, +0.4%] 🔵 -0.5% [-2.0%, +0.8%]
Schelling small 🔵 -2.4% [-2.9%, -1.8%] 🔵 +0.1% [-0.7%, +0.9%]
Schelling large 🔵 -1.0% [-1.6%, -0.4%] 🔵 -1.1% [-2.2%, -0.1%]
WolfSheep small 🔵 -1.0% [-1.5%, -0.4%] 🔵 -3.2% [-7.0%, +0.7%]
WolfSheep large 🔵 -0.0% [-0.6%, +0.7%] 🔵 -2.7% [-3.2%, -2.2%]
SugarscapeG1mt small 🔵 -1.6% [-2.3%, -1.0%] 🔵 -0.4% [-2.2%, +1.6%]
SugarscapeG1mt large 🔵 -2.4% [-3.8%, -0.9%] 🔵 -1.2% [-3.2%, +0.8%]
BoidFlockers small 🔵 -0.5% [-1.2%, +0.1%] 🔵 +0.5% [-0.0%, +0.9%]
BoidFlockers large 🔵 +0.4% [-0.1%, +0.8%] 🔵 -0.7% [-1.0%, -0.4%]

Comment thread mesa/agent.py Outdated

def __str__(self) -> str:
"""Return a string representation of the agent."""
return f"<{self.__class__.__name__} ID:{self.unique_id}>"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"<...>" please remove it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — removed the angle brackets. The format is now aligned with @quaquel's suggestion: f"{self.class.name}, agent_id = {self.unique_id}"

Comment thread mesa/agent.py Outdated
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})"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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__

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@IlamaranMagesh IlamaranMagesh left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments. Overall question is, do we need both str and repr, since the return string looks very similar? Is it not best to keep __repr__ alone as we can't use it anyway for recreation and it's readable?

@quaquel quaquel added the enhancement Release notes label label Apr 4, 2026
@quaquel
quaquel merged commit 43d3680 into mesa:main Apr 4, 2026
9 of 10 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add __str__() to Agent and other Base classes

4 participants