Environmental Science : Python 3.10.4
Put the summary first
In the actual development process , Calling... Is strongly deprecated _
or __
Content at the beginning , image __str__
Except for .
class Hello:
def __init__(self, one, two):
self._one = one
self.__two = two
h = Hello(1, 2)
print(h._one)
print(h.__two)
Output
1
Traceback (most recent call last):
File "/Users/yimt/Code/PycharmProjects/hello-python/hello.py", line 9, in <module>
print(h.__two)
AttributeError: 'Hello' object has no attribute '__two'
class Hello:
def _one(self):
print('one')
def __two(self):
print('two')
h = Hello()
h._one()
h.__two()
Output
Traceback (most recent call last):
File "/Users/yimt/Code/PycharmProjects/hello-python/learn.py", line 1, in <module>
import hello
File "/Users/yimt/Code/PycharmProjects/hello-python/hello.py", line 11, in <module>
h.__two()
AttributeError: 'Hello' object has no attribute '__two'
one
The program runs normally
hello.py
def _one():
print('one')
def __two():
print('two')
main.py
import hello
hello._one()
hello.__two()
Output
one
two