[PYTHON] How to call when using double underscore (Private attribute) for class function

I specified a double underscore for the function in the class and tried it as a Private attribute, but it was recorded when calling the function. <Addition (see pep8): In general, the method of adding two underscores at the beginning of the name is to avoid conflicting attributes of classes designed to be subclassed. Should only be used. >

    class HogeHoge:
        def __init__(self):
            pass

        def __FugaFuga(self):
            iam = "king"
            return iam

I want to print the return value "king" of __FugaFuga at this time,

    iam = _HogeHoge__FugaFuga()
    print(iam)
    #    iam = _HogeHoge__FugaFuga()
    # NameError: name '_HogeHoge__FugaFuga' is not defined

If _HogeHoge__FugaFuga is not found in NameError ...

    HogeHoge = HogeHoge()
    iam = HogeHoge.__FugaFuga()
    #    iam = HogeHoge.__FugaFuga()
    # AttributeError: 'HogeHoge' object has no attribute '__FugaFuga'

Then, when the class and the function are separated, the function __FugaFuga cannot be found ... Success story:

    class HogeHoge:
        def __init__(self):
            pass

        def __FugaFuga(self):
            iam = "common people"
            return iam
    HogeHoge = HogeHoge()
    iam = HogeHoge._HogeHoge__FugaFuga()
    print(iam)

Execution result:

    C:\Users\***\test>python main.py
    common people

It seems that the function of class HogeHoge is _HogeHoge__FugaFuga.

I referred to the article on the following site. Let's master Python underscore (_)! About python encapsulation and mangling PEP8: Python Code Style Guide

Recommended Posts

How to call when using double underscore (Private attribute) for class function
How to call a function
How to deal with SessionNotCreatedException when using Selenium
[Go] How to write or call a function
[For beginners] How to study programming Private memo
How to make unit tests Part.2 Class design for tests
How to exit when using Python in Terminal (Mac)
Where to fix code when using plotly for free
How to write faster when using numpy like deque
[Introduction to Python] How to write repetitive statements using for statements
[Python] How to call a c function from python (ctypes)
[Ansible] How to call variables when creating your own module
How to use the __call__ method in a Python class
How to set the html class attribute in Django's forms.py
How to resolve CSRF Protection when using AngularJS with Django
How to execute the sed command many times using the for statement
Things to watch out for when using default arguments in Python
[Road to Python Intermediate] Call a class instance like a function with __call__
How to display formulas in latex when using sympy (> = 1.4) in Google Colaboratory