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
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.
__init__
is always called when you run class
$python
>>> from train import Hoge
>>> Hoge()
this is init 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
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