| 
 | 
 
 
发表于 2019-1-21 11:56:11
|
显示全部楼层
 
 
 
先看两段代码: 
- class Student(object):
 
 -         def __init__(self,name):
 
 -                 self.name = name
 
 - print(Student('xiao'))
 
  复制代码- <__main__.Student object at 0x000002217177A5F8>
 
  复制代码 
再看下面这个: 
- class Student(object):
 
 -         def __init__(self,name):
 
 -                 self.name = name
 
 -         def __str__(self):
 
 -                 return '学生的成绩是:%s' % self.name
 
 - print(Student('xiao'))
 
  复制代码 
 
 
然后我们查看dir(Student): 
- In [18]: dir(Student)
 
 - Out[18]:
 
 - ['__class__',
 
 -  '__delattr__',
 
 -  '__dict__',
 
 -  '__dir__',
 
 -  '__doc__',
 
 -  '__eq__',
 
 -  '__format__',
 
 -  '__ge__',
 
 -  '__getattribute__',
 
 -  '__gt__',
 
 -  '__hash__',
 
 -  '__init__',
 
 -  '__le__',
 
 -  '__lt__',
 
 -  '__module__',
 
 -  '__ne__',
 
 -  '__new__',
 
 -  '__reduce__',
 
 -  '__reduce_ex__',
 
 -  '__repr__',
 
 -  '__setattr__',
 
 -  '__sizeof__',
 
 -  '__str__',
 
 -  '__subclasshook__',
 
 -  '__weakref__']
 
  复制代码 
 
所以魔法方法可以理解为类的专有方法,有解释器自动执行,如果没有传参数,我是这莫理解的 ,再问就是编译原理的知识了,打住 |   
 
 
 
 |