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]))

猜你喜欢

转载自www.cnblogs.com/sklhtml/p/9456385.html