为了账号安全,请及时绑定邮箱和手机立即绑定

python进阶

廖雪峰 移动开发工程师
难度中级
时长 3小时33分
学习人数
综合评分9.20
575人评价 查看评价
9.6 内容实用
9.0 简洁易懂
9.0 逻辑清晰
  • 首先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
    查看全部
    0 采集 收起 来源:python中 @property

    2018-03-22

  • 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
    查看全部
    0 采集 收起 来源:python中 @property

    2018-03-22

  • 利用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 采集 收起 来源:python中 @property

    2016-07-26

举报

0/150
提交
取消
课程须知
本课程是Python入门的后续课程 1、掌握Python编程的基础知识 2、掌握Python函数的编写 3、对面向对象编程有所了解更佳
老师告诉你能学到什么?
1、什么是函数式编程 2、Python的函数式编程特点 3、Python的模块 4、Python面向对象编程 5、Python强大的定制类

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!