self
发布日期:2021-05-09 14:26:09 浏览次数:16 分类:精选文章

本文共 3619 字,大约阅读时间需要 12 分钟。

������Python���������������������������

���Python���������������������������������������������������������������������������������������������������������������������������������������self���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

1. ������������������self������

���Python���������������������������������������������������self���������������

  • **���������**������������������������������������������������������������������������������������������������������������������

  • **���������**������������������������self���MyClass���������������������������������������name������������������

class MyClass:
def my_method(self, name):
self.name = name
  • **���������������������**���������������������self.������������������self.name = 'tester'������������name��������������������������������������������� �������������������

2. ������������������������������������������������

������}}}}}}���������������Python������������������������������������������������������������������������

  • **������������������������**���������������������self���������������������������������@classmethod���@staticmethod������������������������������������
class MyClass:
my_var = 'class_var' # ���������
def __init__(self):
self.my_var = 'instance_var' # ������������
  • **���������������������������**������������self���������������������������������
def func():
var = 'local_var' # ������������
  • **������������������**������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

3. ������������

������������������������������������������������������������

# ��� nc ��� name ��� self ������
class nc:
def __init__(self):
self.name = 'tester'
# ��� mc ��� name ������ self ������
class mc:
def __init__(self):
name = 'tesster'
# ���������������
nc = nc()
mc = mc()
# ������ . ���������������
print(nc.name) # ������ 'tester'
try:
print(mc.name)
except AttributeError:
print('mc ��������� name ������')
���������������mc.nameThrow AttributeError: 'mc' object has no attribute 'name'
��������������� mc ��� __init__ ������������ name ������������������������������������������������������������������������������

4. ������������������������������

���������������������������������������������������������������������

  • **���������������������**������������������������������������������������������������������������������������������������������������������
class MyClass:
def my_method(self):
name = 'local_name' # ������������ name ���������������
print(MyClass.name) # ������ MyClass.name ������������������������
  • **���������������**��������������������������� self���������������������������������������������������

���������

������������������������������������������������������

  • **���������������������������**���������������������������������������self.���������������������������������������������������������������������������

  • **���������������������������������**������������������������������������������������������������������������������������

  • **���������������������������������������������**������������������������������������������������������

���������������������������������Python���������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:python 类
下一篇:常见数据集与torch实现

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月22日 15时05分41秒