Python进阶-类的特殊成员
来源:互联网 发布:cet照片采集软件 编辑:程序博客网 时间:2024/05/20 06:05
Python
类的特殊成员
在 python 中,有许多形如__init__
的方法,这是类的特殊成员,这样的特殊成员还有很多,接下来将介绍一下常用的特殊成员。
__init__
类的构造方法,通过类创建对象时,自动触发执行。前面例子已经讲过,这里面就不在赘述了。
__del__
析构方法,当对象在内存中被释放时,自动触发执行。
注意:此方法一般无须定义,因为和java 类似,Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的。
__str__
class Person(object): def __init__(self, name, gender): self.name = name self.gender = genderclass Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = scorestudent = Student('messi', 'male', 88)print (student)#<__main__.Student object at 0x10b81e470>
如果要把一个类的实例变成 str,就需要实现特殊方法__str__()
class Person(object): def __init__(self, name, gender): self.name = name self.gender = genderclass Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = score def __str__(self): return '(Student: %s, %s, %s)' % (self.name, self.gender, self.score)s = Student('Bob', 'male', 88)print (s)#(Student: Bob, male, 88)
__eq__
自定义__eq__
函数可以按照我们自己定义的规则来判断两个实例是否相同。
class Student(object): def __init__(self, name, score): self.name = name self.score = score def __str__(self): return '(%s: %s)' % (self.name, self.score) def __eq__(self, other): if isinstance(other, Student): if self.score == other.score: return True else: return Falsestudent1 = Student('Tim', 99)student2 = Student('Bob', 99)print(student1 == student2)#True
上面示例说明只要student1和student2的成绩相同时,我们就说两个实例相同。当没有自定义__eq__
函数时,返回的结果是 False,这是调用了 object中的__eq__
。
__add__
class Student(object): def __init__(self, name, score): self.name = name self.score = score def __str__(self): return '(%s: %s)' % (self.name, self.score) def __add__(self, other): if isinstance(other, Student): return self.score + other.score else: raise Exception('the type of other must be Programer')student1 = Student('Tim', 99)student2 = Student('Bob', 99)print(student1 + student2)#198
__len()__
如果一个类表现得像一个list,要获取有多少个元素,就得用 len() 函数。
要让 len() 函数工作正常,类必须提供一个特殊方法len(),它返回元素的个数。
- 根据num计算出斐波那契数列的前N个元素
class Fib(object): def __init__(self, num): a, b, L = 0, 1, [] for n in range(num): L.append(a) a, b = b, a + b self.numbers = L def __str__(self): return str(self.numbers) def __len__(self): return len(self.numbers)fib=Fib(10)print(fib)#[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]print(len(fib))#10
__dict__
获取类或对象中的所有成员。
class Student(object): def __init__(self, name, score): self.name = name self.score = scoreprint(Student.__dict__)#{'__doc__': None, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__init__': <function Student.__init__ at 0x100fc21e0>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__module__': '__main__'}student1 = Student('Tim', 99)print(student1.__dict__)#{'name': 'Tim', 'score': 99}
0 0
- Python进阶-类的特殊成员
- Python类的特殊成员
- Python类的特殊成员5
- Python类的特殊成员方法
- python入门(三十二):类的特殊成员
- 类的特殊成员
- [Python进阶-5]类的特殊方法
- Python 进阶特殊方法
- 类的特殊数据成员
- 类的特殊成员方法
- Python的特殊类成员6;此需要注意是Python3
- 类中特殊成员变量的初始化
- as类成员变量的特殊用法
- C++类的特殊数据成员
- 学习C++ -> 类的特殊数据成员
- 类中特殊成员变量的初始化
- 类中特殊成员变量的初始化
- 类的特殊成员变量初始化
- c++ nullprt
- Android开发常用工具类
- c++ 宏-T
- 商业数据分析(对Excel的处理)
- 中国农业大学Linux系统登录网关
- Python进阶-类的特殊成员
- maven简介
- 文章标题
- vector类型变量在main之前初始化
- 关于c++11的新特性
- View总结-事件分发机制
- main函数执行前、后再执行的代码
- 苏嵌11 16.11.10
- 解决IE兼容问题,IE6,IE7,IE8,IE9,IE10