python __str__(),__dir__(),__doc__
来源:互联网 发布:plsql导入sql文件乱码 编辑:程序博客网 时间:2024/05/20 19:15
对于一个类的属性查看,用dir
__str__() 是介绍这个类的信息
__doc__ 这个是文档字符串,也是介绍
对于dir的用法
>>> import os>>> try:... a = 1/0... except Exception,reason1:... print "can't /"...can't />>> print reason1integer division or modulo by zero>>> print reason1.__str__()integer division or modulo by zero>>> dir(reason1)['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__getslice__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', 'args', 'message']>>> print reason1.__doc__()Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object is not callable>>> print reason1.__doc__Second argument to a division or modulo operation was zero.>>> print reason1.__str__<method-wrapper '__str__' of exceptions.ZeroDivisionError object at 0x10c095b90>>>> print reason1.__calss__Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: 'exceptions.ZeroDivisionError' object has no attribute '__calss__'>>> print reason1.__class__<type 'exceptions.ZeroDivisionError'>>>> type(reason1.__doc__)<type 'str'>>>>
对于dir,它显示的是类的所有的属性和方法。
中文说明:不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。
如果参数包含方法__dir__(),该方法被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
0 0
- python __str__(),__dir__(),__doc__
- python __doc__
- Python dir() 和 __dir__
- python __doc__
- python中的__doc__,__name__
- Python中的__doc__
- __doc__
- __doc__
- python 文档资源 dir __doc__
- python: 理解__str__
- python - __str__ 和 __repr__
- python 之 __str__
- python 之 __str__
- python: 理解__str__
- python __str__ , __repr__区别
- python - __str__ 和 __repr__
- python - __str__ 和 __repr__
- python - __str__ 和 __repr__
- 找出错误:类型转换
- arm-linux-gcc
- java中float类型数据的存储
- java面试题:
- java面试题:
- python __str__(),__dir__(),__doc__
- java知识点
- java collection
- java(对象调用内存图)
- java创建对象内存图
- 深入浅出讲解傅里叶变换
- java的Static关键字使用内存图解:
- 一个java类的完整示例:
- 使用tcpdump观察telnet 访问网页实验