1

I was wandering in the source code of the fabulous python-chess library when I saw the following code:

def _reset_board(self):
    # code...

def reset_board(self):
    self._reset_board()

The reset_board() function only does one thing, call its private counterpart. Is there a reason behind this? Wouldn't putting the code directly in the private function be faster due to python not have to resolve the name _reset_board()?

Null User
  • 338
  • 1
  • 4
  • 17
  • 2
    I'll say it is primarily opinion based. – Moinuddin Quadri Feb 14 '18 at 16:01
  • Most of the time dunder name means don't mess with this code if you don't know exactly what you are doing. so my guess is, if there's someone who wants to hook into `reset_board()` to do something before or after the `reset_board()` they can in a spot that is relevantly clean, without messing with the actual function – MooingRawr Feb 14 '18 at 16:03
  • @MoinuddinQuadri And what are the arguments supporting it? Because I cannot seem to be able to think of any. – Ma0 Feb 14 '18 at 16:03
  • @Ev.Kounis argument supporting what? That it is opinion based? – Moinuddin Quadri Feb 14 '18 at 16:11
  • @MoinuddinQuadri Opinion based implies that are people who find such a construct useful and other who don't. The latter group I understand; I find it unnecessary too. What are the arguments of the people supporting it? – Ma0 Feb 14 '18 at 16:16

2 Answers2

6

_reset_board exists so it can be called from both reset_board and __init__. __init__ can't call self.reset_board, because that method is overridden in subclasses, and __init__ wants to call the specific _reset_board implementation from its own class. (Subclass reset_board implementations may depend on initialization that hasn't happened yet, among other problems.)

user2357112
  • 260,549
  • 28
  • 431
  • 505
0

I agree with you, here this _reset_board is not necessary. The author probably did some wrapping/cleaning in the reset_board method before, removed it, and didn't take the time to remove the _reset_board. Or maybe he plans to add some wrapping/cleaning in there in the future.

Some project also may generate Documentation automatically based on the code, and may skip functions/method that start with a _, and he may not want to publish any documentation for this function, but being open source, it's probably not the real reason.

Null User
  • 338
  • 1
  • 4
  • 17