Skip to content

Commit b016906

Browse files
committed
Add something about _repr_ to the docs
1 parent 8658e83 commit b016906

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/contributing.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,33 @@ Here is the :code:`reset` method which takes care of resetting this in between r
114114
self.grudged = False
115115
self.grudge_memory = 0
116116

117+
118+
You can also modify the name of the strategy with the `__repr__` method, which is invoked
119+
when `str` is applied to a player instance. For example, the player `Random` takes a
120+
parameter `p` for how often it cooperates, and the `__repr__` method adds the value
121+
of this parameter to the name::
122+
123+
def __repr__(self):
124+
return "%s: %s" % (self.name, round(self.p, 2))
125+
126+
Now we have separate names for different instantiations::
127+
128+
import axelrod
129+
player1 = axelrod.Random(p=0.5)
130+
player2 = axelrod.Random(p=0.1)
131+
print(str(player1))
132+
print(str(player2))
133+
134+
This produces the following output::
135+
136+
'Random: 0.5'
137+
'Random: 0.1'
138+
139+
This helps distinguish players in tournaments that have multiple instances of the
140+
same strategy. If you modify the `__repr__` method of player, be sure to add an
141+
appropriate test.
142+
143+
117144
Adding the strategy to the library
118145
''''''''''''''''''''''''''''''''''
119146

0 commit comments

Comments
 (0)