[PYTHON] About _ and __

Describe it as a memo.

About "_function"

Python has virtually no Private attribute, but you can define a function internally with _

module/func.py


def _func():
  return something

module/__init__.py


from .module import func

In the above case

test.py


from module import *

print(func._func())
ImportError: cannot import name 'func' from 'module.func'

From M import * does not import functions that start with one underscore But,

module/func.py


class Net():

  def _func(self):
    return 'something'

module/__init__.py


from .module import Net

In the above case

test.py


from module import *

print(Net()._func())

Can be called.

That is, with __init__.py in the call to ʻimport *`,

module/__init__.py


from .module import _variable

Because it cannot be described.

※important _Func () can be used unless it is described as import *

About "__function"

Of course, it also has the functions explained above and calls the name mangling mechanism. Originally, the name mangling mechanism This is for the purpose of avoiding name conflicts between the parent class and the child class.

module/func.py


class Net():

  def __func(self):
    return 'something'

module/__init__.py


from .module import Net

In the above case

test.py


from module import *

print(Net()._Net_func())

Instead of calling _Net_func (), you can create a pseudo private attribute

About "\ _ \ _function \ _ \ _"

Become a magic method.

There are existing magic methods such as __init__, __call__, and __iter__. This allows you to write your class neatly. Do not define a new one yourself in normal development.

Recommended Posts

About _ and __
About cross-validation and F-number
This and that about pd.DataFrame
Linux (about files and directories)
About python objects and classes
About Python variables and objects
About LINUX files and processes
About Raid group and LUN
About fork () function and execve () function
About Django's deconstruct and deconstructible
About Python, len () and randint ()
About Python datetime and timezone
About Sharpe Ratio and Sortino Ratio
About Python and regular expressions
About http.Handle () and http.NewServeMux (). Handle ()
Python # About reference and copy
About Python sort () and reverse ()
About LangID
About virtiofs
About python-apt
About Permission
About sklearn.preprocessing.Imputer
About gunicorn
About installing Pwntools and Python2 series
About requirements.txt
About locale
About Opencv ②
Summary and common errors about cron
About axis = 0, axis = 1
About Opencv ③
About python dict and sorted functions
About import
About dtypes in Python and Cython
About MkDocs themes and their customs
About Python pickle (cPickle) and marshal
[Python] About Executor and Future classes
About Python, from and import, as
About numpy
About pip
About Linux
About numpy.newaxis
About endian
About Linux
About import
About Opencv ①
About Linux
About Linux ①
About cv2.imread
About wxPython
About package management with conda and pip
Memorandum about regression and binary classification metrics
About sensor_mode and angle of view of picamera
About errors during PyInstaller installation and execution
About PyQt signal, connect and lambda expressions
Personal notes and links about machine learning ① (Machine learning)
[SSH] About public key authentication and EC2
Finding out about file permissions and superuser
About the relationship between Git and GitHub
A story about Python pop and append
About AntiDDoS Premium and Special Features TOA
Talking about old and new Python classes