python 内建函数
#
# __geratteibute__
class Itcast(object):
def __init__(self,subject1):
self.subject1 = subject1
self.subject2 = 'cpp'
# 属性访问时拦截器,打log日志
def __getattribute__(self,obj):
print("=====1>%s"%obj)
if obj == 'subject1':
print('log subjiect')
return 'redirect python'
else:
temp = object.__getattribute__(self,obj)
print("====2>%s"%str(temp))
return temp
def show(self):
print("this is Itcast")
s = Itcast("python")
print(s.subject1)
print(s.subject2)
# =====1>subject1
# log subjiect
# redirect python
# =====1>subject2
# ====2>cpp
# cpp
s.show()
# =====1>show
# ====2><bound method Itcast.show of <__main__.Itcast object at 0x000001BC04FE5AC8>>
# this is Itcast
# 1.先获取show属性对应的结果,,,,,应该是一个方法
# 2.方法()
class Person(object):
def __getattribute__(self,obj):
print("----test----")
if obj.startswith("a"): #startswith以什me开头
return 'r'
else:
# return self.test
return object.__getattribute__(self,obj)
def test():
print("hehehe")
# 访问属性之前肯定会访问__getattribute__
t = Person()
# t.a #hahah
# t.b #会让程序死掉
# 内建函数
# range
# python2中返回直接是一个列表
# python3中不会 直接返回一个列表
# xrange 节省内存空间
# map函数
# map函数会根据提供的函数对指定序列做映射
# map(...)
# map(function,sequence[,sequence,...]) ->list
# function:是一个函数
# sequence:是一个或多个序列,取决于function需要几个参数
# 返回值是一个list
# 参数序列中的每一个元素分别调用function函数,返回包含每次function函数返回值得list
# 函数需要一个参数
# map(lambda x: x*x,[1,2,3])
# map(lambda x,y:x+y,[1,2,3],[4,5,6])
def f1(x,y):
return (x,y)
l1 = [0,1,2,3,4,5,6]
l2 = [0,1,2,3,4,5,6]
l3 = map(f1,l1,l2)
print(l3)
# <map object at 0x000001EDB5C75DA0>
# filter函数
a = filter(lambda x:x%2, [1,2,3,4])
print(a)
# <filter object at 0x000001BA5AFF5E10>
for tmp in a:
print(tmp)
# 1
# 3
filter(None,"she")
# reduce函数
# reduce函数,reduce函数会对参数序列中元素进行累积
# b = reduce(lambda x,y: x+y, [1,2,3,4])
# print(b)
# reduce(lambda x,y: x+y,["aa","bb","cc"],"dd")
# "ddaabbcc"
c = [1,3,2,9,76,54243]
c.sort()
print(c)
c = [1,3,2,9,76,54243]
# sorted函数
sorted([1,4,3,2])
print(sorted([1,4,3,2]))
最新文章
- c++中的peek函数
- LA 6856 Circle of digits 解题报告
- Web前端开发工程师为什么讨厌IE6!
- Android中Activity之间访问互传参数
- 关于oracle数据库备份还原-impdp,expdp
- 再起航,我的学习笔记之JavaScript设计模式03
- less基础语法
- 调用链监控 CAT 之 入门
- framework7 入门(数据绑定)
- pip 安装问题
- C语言之指针若干问题
- ES6 generators in depth 一(译)
- 章节二、1-java概述-数据类型
- hibernate框架学习之数据查询(QBC)
- Spring整合MyBatis(简单登录Demo)
- 05python下
- wait()和notify()的理解与使用
- Linux命令区
- oracle 数据库io 异常,错误代码17002 解决办法
- WIN2008服务器不能复制粘贴怎么办