
本文共 3145 字,大约阅读时间需要 10 分钟。
Python���
������Python������������������������������������
���Python������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���Pub���```pythonclass Pub(): _name = 'protected���������������' __info = '���������������������'
def _func(self): print("������������protected���������������") print(self._name) def __func2(self): print('���������������������������������') def get(self): return self.__info
���������������:
```pythonp = Pub()p._name = 'changed' # ���������print(p._name) # protected���������������# print(p.__info) # ���������'Pub' object has no attribute '__info'p._func() # ������������protected���������������# p.__func2() # ���������'Pub' object has no attribute '__func2'print(p._Pub__info) # ���������������������print(p._Pub__func2()) # ���������������������������������
���������
- ������������������������������������������������������������������������������- ������������������������������������������������������������������������
1. ���������������������������������������������������
2. ���������������������������������������������������������������������������������
3. ������������������������������������������������������������������������������������������������������isinstance()���������������������������
���������������������
- ��������������������������������������� - ������������������������������������������������������������������������```pythonclass Animal: pass
class Dog(Animal):pass
���������������������
```pythona = Animal() # Animal������b = Dog() # Dog������c = list() # list������print(isinstance(b, Animal)) # Trueprint(isinstance(a, Dog)) # Falseprint(isinstance(b, Dog)) # True
���������������������������������������������
������������������������������������
1. ������������������������������������������������������������������������
���������```pythondef func(): a = 1 _b = 2 __c = 3 print(a, _b, __c)
def func2(): _b = 5 __c = 6 print(_b, __c)
func2()func() # ������: 1 2 3 5 6
���������������������������������������������������������
2. ������������������������������������������������
������������������������������������������������������������������������������������������������������```pythonclass A: name = 'wang' _age = 22 __sex = 'men' a = A()print(a.name) # wang# a.name = 'liu' ���������������������������������������������������a.nameprint(a._age) # 22print(a.__sex) # ���������������������__sex������print(a._A__sex) # ������������������������������������������������������������������������������������
3. ������������������nonlocal���������������������
- ��������������������������������������������������������������������� - ������������������������������������������������������ - ���������������������������������������������������������������������```pythondef outer(): x = 10 def inner(): print(x) x += 1 print("inner:', x) inner()
outer()
发表评论
最新留言
关于作者
