-
首先s就是一个list,s[0:1]就是s这个list中的字符串的首字母,可以通过return来实现返回的值。查看全部
-
因为在一开始的时候调用了math,所以在使用平方根的时候要写: math.sqrt查看全部
-
可以接受函数和变量的函数就是高阶函数查看全部
-
s.title()的作用就是把字符串第一个字母变成大写,其余变成小写查看全部
-
错误处理: try: print Person.__count except AttributeError: print 'attributeError'查看全部
-
Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数。 使用 decorator 用Python提供的 @ 语法,这样可以避免手动编写 f = decorate(f) 这样的代码。 考察一个@log的定义: def log(f): def fn(x): print 'call ' + f.__name__ + '()...' return f(x) return fn 对于阶乘函数,@log工作得很好: @log def factorial(n): return reduce(lambda x,y: x*y, range(1, n+1)) print factorial(10) 结果: call factorial()... 3628800 但是,对于参数不是一个的函数,调用将报错: @log def add(x, y): return x + y print add(1, 2) 结果: Traceback (most recent call last): File "test.py", line 15, in <module> print add(1,2) TypeError: fn() takes exactly 1 argument (2 given) 因为 add() 函数需要传入两个参数,但是 @log 写死了只含一个参数的返回函数。 要让 @log 自适应任何参数定义的函数,可以利用Python的 *args 和 **kw,保证任意个数的参数总是能正常调用: def log(f): def fn(*args, **kw): print 'call ' + f.__name__ + '()...' return f(*args, **kw) return fn 现在,对于任意函数,@log 都能正常工作。查看全部
-
正确的写法: class C(A, B) def __init__(self, a, b): A.__init__(self, a) B.__init__(self, b) 建议养成习惯,不要使用super()这个函数,即便是单继承,也使用上面的方式查看全部
-
匿名函数 高阶函数可以接收函数做参数,有些时候,我们不需要显式地定义函数,直接传入匿名函数更方便。 在Python中,对匿名函数提供了有限支持。还是以map()函数为例,计算 f(x)=x2 时,除了定义一个f(x)的函数外,还可以直接传入匿名函数: >>> map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]) [1, 4, 9, 16, 25, 36, 49, 64, 81] 通过对比可以看出,匿名函数 lambda x: x * x 实际上就是: def f(x): return x * x 关键字lambda 表示匿名函数,冒号前面的 x 表示函数参数。 匿名函数有个限制,就是只能有一个表达式,不写return,返回值就是该表达式的结果。 使用匿名函数,可以不必定义函数名,直接创建一个函数对象,很多时候可以简化代码: >>> sorted([1, 3, 9, 5, 0], lambda x,y: -cmp(x,y)) [9, 5, 3, 1, 0] 返回函数的时候,也可以返回匿名函数: >>> myabs = lambda x: -x if x < 0 else x >>> myabs(-1) 1 >>> myabs(1) 1查看全部
-
sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted()也是一个高阶函数,它可以接收一个比较函数来实现自定义排序,比较函数的定义是,传入两个待比较的元素 x, y,如果 x 应该排在 y 的前面,返回 -1,如果 x 应该排在 y 的后面,返回 1。如果 x 和 y 相等,返回 0。查看全部
-
orted()也可以对字符串进行排序,字符串默认按照ASCII大小来比较: >>> sorted(['bob', 'about', 'Zoo', 'Credit']) ['Credit', 'Zoo', 'about', 'bob']查看全部
-
sorted() 并传入 reversed_cmp 就可以实现倒序排序: >>> sorted([36, 5, 12, 9, 21], reversed_cmp) [36, 21, 12, 9, 5]查看全部
-
class Student(object): def __init__(self,name,score): self.name = name self.__score = score @property def score(self): return self.__socre @score.setter def score(self,score): if score < 0 or score > 100: raise ValueError('invalid score') self.__score = score查看全部
-
class Student(object): def __init__(self,name,score): self.name = name self.__score = score def get_score(self): return self.__score def set__score(self,score): if score < 0 or score > 100: raise ValueError('invaild score') self.__score = score查看全部
-
利用filter()过滤出1~100中平方根是整数的数 import math def is_sqr(x): return math.sqrt(x)%2==1 or math.sqrt(x)%2==0 print filter(is_sqr, range(1, 101))查看全部
-
考察Student类 class Student(object): def __init__(self,name,score): self.name = name self.score = score 当我们想要修改一个Student的score属性时,可以这么写 s = Student('Bob',59) s.score = 60查看全部
举报
0/150
提交
取消