What is the point of this code?

I tried to create a python shell for the tk extension, so I looked at Tkinter.py to find out how to do this.

When I looked at this file, I found that the following pattern appears many times: the internal method (indicated by the leading "_" in the method name) is defined, then the public method is defined only as an internal method.

I want to know what is the use of this.

For example, in the code for the Misc class:

def _register(self, func, subst=None, needcleanup=1):
    # doc string and implementations is removed since it not relevant
register = _register

Thanks.

+3
source share
3 answers

Sometimes you can change the behavior of a method. For example, I could do this (hypothetically in the Misc class):

def _another_register(self, func, subst=None, needcleanup=1):
    ...

def change_register(self):
    self.register = self._another_register

def restore_register(self):
    self.register = self._register

( , ).

+8

PEP8

,    (   ):

_single_leading_underscore: " ". . " M import *" .

+2

, , , , , . , , - , , , stubbed out, , .

+1

Source: https://habr.com/ru/post/1712420/


All Articles