目录:

一、 __getattribute__

二、__str__,__repr__,__format__

三、__doc__

四、__module__和__class__

一、 __getattribute__                                                               

 class Foo:
def __init__(self,x):
self.x=x def __getattr__(self, item):
print('执行的是我')
# return self.__dict__[item] f1=Foo(10)
print(f1.x)
f1.xxxxxx #不存在的属性访问,触发__getattr__ 回顾__getattr__

回顾__getattr__

 class Foo:
def __init__(self,x):
self.x=x def __getattribute__(self, item):
print('不管是否存在,我都会执行') f1=Foo(10)
f1.x
f1.xxxxxx __getattribute__

__getattribute__

 #_*_coding:utf-8_*_
__author__ = 'Linhaifeng' class Foo:
def __init__(self,x):
self.x=x def __getattr__(self, item):
print('执行的是我')
# return self.__dict__[item]
def __getattribute__(self, item):
print('不管是否存在,我都会执行')
raise AttributeError('哈哈') f1=Foo(10)
f1.x
f1.xxxxxx #当__getattribute__与__getattr__同时存在,只会执行__getattrbute__,除非__getattribute__在执行过程中抛出异常AttributeError 二者同时出现

二者同时出现

二、__str__,__repr__,__format__                                           

改变对象的字符串显示__str__,__repr__

自定制格式化字符串__format__

 #_*_coding:utf-8_*_
__author__ = 'Linhaifeng'
format_dict={
'nat':'{obj.name}-{obj.addr}-{obj.type}',#学校名-学校地址-学校类型
'tna':'{obj.type}:{obj.name}:{obj.addr}',#学校类型:学校名:学校地址
'tan':'{obj.type}/{obj.addr}/{obj.name}',#学校类型/学校地址/学校名
}
class School:
def __init__(self,name,addr,type):
self.name=name
self.addr=addr
self.type=type def __repr__(self):
return 'School(%s,%s)' %(self.name,self.addr)
def __str__(self):
return '(%s,%s)' %(self.name,self.addr) def __format__(self, format_spec):
# if format_spec
if not format_spec or format_spec not in format_dict:
format_spec='nat'
fmt=format_dict[format_spec]
return fmt.format(obj=self) s1=School('oldboy1','北京','私立')
print('from repr: ',repr(s1))
print('from str: ',str(s1))
print(s1) '''
str函数或者print函数--->obj.__str__()
repr或者交互式解释器--->obj.__repr__()
如果__str__没有被定义,那么就会使用__repr__来代替输出
注意:这俩方法的返回值必须是字符串,否则抛出异常
'''
print(format(s1,'nat'))
print(format(s1,'tna'))
print(format(s1,'tan'))
print(format(s1,'asfdasdffd'))
 date_dic={
'ymd':'{0.year}:{0.month}:{0.day}',
'dmy':'{0.day}/{0.month}/{0.year}',
'mdy':'{0.month}-{0.day}-{0.year}',
}
class Date:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day def __format__(self, format_spec):
if not format_spec or format_spec not in date_dic:
format_spec='ymd'
fmt=date_dic[format_spec]
return fmt.format(self) d1=Date(2016,12,29)
print(format(d1))
print('{:mdy}'.format(d1)) 自定义format练习

自定义format练习

 #_*_coding:utf-8_*_
__author__ = 'Linhaifeng' class A:
pass class B(A):
pass print(issubclass(B,A)) #B是A的子类,返回True a1=A()
print(isinstance(a1,A)) #a1是A的实例 issubclass和isinstance

issubclass和isinstance

三、__doc__                                                                              

class Foo:
'我是描述信息'
pass print(Foo.__doc__)

它类的信息描述

class Foo:
'我是描述信息'
pass class Bar(Foo):
pass
print(Bar.__doc__) #该属性无法继承给子类 该属性无法被继承

该属性无法被继承

四、__module__和__class__                                                     

__module__ 表示当前操作的对象在那个模块

__class__     表示当前操作的对象的类是什么

#!/usr/bin/env python
# -*- coding:utf-8 -*- class C: def __init__(self):
self.name = ‘SB' lib/aa.py

lib/aa.py

from lib.aa import C

obj = C()
print obj.__module__ # 输出 lib.aa,即:输出模块
print obj.__class__ # 输出 lib.aa.C,即:输出类

index.py

最新文章

  1. 鼠标上下滑动总是放大缩小页面,按住ctrl+0
  2. 设计模式之里氏代换原则(LSP)
  3. 放养的小爬虫--京东定向爬虫(AJAX获取价格数据)
  4. SQL语句的使用
  5. String类的构造方法详解
  6. 为什么学习webdriver
  7. var 0bj = []声明报错的小问题
  8. SQL中对XML的处理
  9. Linux的set
  10. 2015.11.16JQuery 隐藏,显示按钮.
  11. Ext 面向对象程序设计 入门篇
  12. Python进阶之匿名函数(关键词lambda)
  13. 【译】The Accidental DBA:Troubleshooting
  14. 软件测试之BUG分析定位概述(QA如何分析定位BUG)
  15. ZeroC Ice IceGrid Node和IceGrid
  16. 使用element-ui遇到的各种小问题
  17. wxPython制作跑monkey工具(python3)-带显示设备列表界面
  18. 通过一个小Trick实现shader的像素识别/统计操作
  19. Vue.js开始第一个项目
  20. Path Sum II leetcode java

热门文章

  1. PHP版本的讲解
  2. Java EE JSP编程基础
  3. AnyProxy对搜狐汽车app抓包
  4. 存储过程关于LOOP循环问题
  5. NO.02---聊聊Vue提升
  6. [ Continuously Update ] The Paper List of Seq2Seq Tasks ( including Attention Mechanism )
  7. springboot集成jpa,在postgresql数据库中创建主键自增表
  8. GitLab 搭建与使用
  9. visual studio 2010 和 VSS(Visual SourceSafe)的连接使用
  10. rest_framework之渲染器