[Python] What are the two underscore (underscore) functions before?

Sometimes there are two underscores before a function in class

    def __hogehoge():
        print('Hello.')

I didn't know what it meant, so a memorandum Conclusion: can only be called within that class

Try to practice

Let's actually try it ~ Prepare the following sample

train.py


class Hoge:
    def __init__(self):
        print('this is init function.')
        
    def public():
        print('this is public function.')
    
    def __private():
        print('this is private function.')

Move to the directory where this file is located in [Terminal], and then execute as follows.

Execution of Hoge class

__init__ is always called when you run class

$python
>>> from train import Hoge 
>>> Hoge()
this is init function.

Execution of public function

Enter the following in the continuation of the terminal

>>> Hoge.public()
this is public function.

Functions with nothing attached can be called without problems

Execution of private function

Enter the following in the continuation of the terminal

>>> Hoge.__private()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Hoge' has no attribute '__private'

External call failure

Postscript: 19/12/4 I received a comment. If you really want to call it, you can call it by typing Foo._Foo__a! Thank you for the information.

Recommended Posts

[Python] What are the two underscore (underscore) functions before?
What is the python underscore (_) for?
Not surprisingly known! ?? What about the arguments of built-in functions? What school are you from? [Python]
[Python] What is @? (About the decorator)
[Python] What are @classmethods and decorators?
[python] What is the sorted key?
Python functions
python underscore
What is "mahjong" in the Python library? ??
Python open and io.open are the same
The contents of the Python tutorial (Chapter 5) are itemized.
When do python functions affect the caller's arguments?
The contents of the Python tutorial (Chapter 4) are itemized.
The contents of the Python tutorial (Chapter 2) are itemized.
What is wheezy in the Docker Python image?
The contents of the Python tutorial (Chapter 8) are itemized.
The contents of the Python tutorial (Chapter 1) are itemized.
[Minecraft] What are the important items in survival?
The contents of the Python tutorial (Chapter 10) are itemized.
What are you comparing with Python is and ==?
[Note] About the role of underscore "_" in Python
The contents of the Python tutorial (Chapter 6) are itemized.
The contents of the Python tutorial (Chapter 3) are itemized.
What are python tuples and * args after all?
What are you using when testing with Python?
Python underscore variable
What is python
#Python basics (functions)
[Beginner] Python functions
Python Easy-to-use functions
Python basics: functions
What is Python
What I got into Python for the first time
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
Financial Forecasting Feature Engineering: What are the features in financial forecasting?
Rotate and scale the image before cropping [python] [OpenCV]
Using the LibreOffice app in Python (1) Where are the macros?
What does the last () in a function mean in Python?
I tried to summarize what python strong people are doing in the competition professional neighborhood
What kind of environment should people who are learning Python for the first time build?