为了账号安全,请及时绑定邮箱和手机立即绑定
慕课网数字资源数据库体验端
python进阶_学习笔记_慕课网
为了账号安全,请及时绑定邮箱和手机立即绑定

python进阶

廖雪峰 移动开发工程师
难度中级
时长 3小时33分
  • map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回。 map()不改变原有的list,而是会返回一个新的list
    查看全部
  • 例子就是定义了一个能接收高阶函数的函数add,传入x,y两个普通变量,f传入一个函数abs
    查看全部
  • 高阶函数,就是能接受函数作为变量传入的函数
    查看全部
  • 变量可以指向一个函数,比如f = abs<br> f(-20)得到20 函数名就是指向函数的变量
    查看全部
  • python支持的函数式编程的特点
    查看全部
  • python支持的函数式编程的特点
    查看全部
  • python支持的函数式编程的特点
    查看全部
  • 函数式编程,注意不是函数编程
    查看全部
  • 像这种内层函数引用了外层函数的变量(参数也算变量),然后返回内层函数的情况,称为闭包(Closure)。 闭包的特点是返回的函数还引用了外层函数的局部变量,所以,要正确使用闭包,就要确保引用的局部变量在函数返回后不能变。
    查看全部
    0 采集 收起 来源:python中闭包

    2016-05-04

  • 一个类实例也可以变成一个可调用对象,只需要实现一个特殊方法__call__()。 我们把 Person 类变成一个可调用对象: class Person(object): (4个空格) def __init__(self, name, gender): (4个空格) self.name = name (4个空格) self.gender = gender (4个空格) def __call__(self, friend): (4个空格) print 'My name is %s...' % self.name (4个空格) print 'My friend is %s...' % friend 现在可以对 Person 实例直接调用: >>> p = Person('Bob', 'male') >>> p('Tim') My name is Bob... My friend is Tim...
    查看全部
    0 采集 收起 来源:python中 __call__

    2018-03-22

  • __slots__的目的是限制当前类所能拥有的属性,如果不需要添加任意动态的属性,使用__slots__也能节省内存。 class Student(object): (4个空格) __slots__ = ('name', 'gender', 'score') (4个空格) def __init__(self, name, gender, score): (8个空格) self.name = name (8个空格) self.gender = gender (8个空格) self.score = score
    查看全部
    0 采集 收起 来源:python中 __slots__

    2016-05-03

  • 用装饰器函数把 get/set 方法“装饰”成属性调用: class Student(object): (4个空格) def __init__(self, name, score): (8个空格) self.name = name (8个空格) self.__score = score (4个空格) @property (4个空格) def score(self): (8个空格) return self.__score (4个空格) @score.setter (4个空格) def score(self, score): (8个空格) if score < 0 or score > 100: (12个空格) raise ValueError('invalid score') (4个空格) self.__score = score 注意: 第一个score(self)是get方法,用@property装饰,第二个score(self, score)是set方法,用@score.setter装饰,@score.setter是前一个@property装饰后的副产品。 现在,就可以像使用属性一样设置score了: >>> s = Student('Bob', 59) >>> s.score = 60 >>> print s.score 60 >>> s.score = 1000 Traceback (most recent call last): ... ValueError: invalid score
    查看全部
    0 采集 收起 来源:python中 @property

    2018-03-22

  • get/set方法,规范数据的获取和修改 class Student(object): (4个空格) def __init__(self, name, score): (8个空格) self.name = name (8个空格) self.__score = score (4个空格) def get_score(self): (8个空格) return self.__score (4个空格) def set_score(self, score): (8个空格) if score < 0 or score > 100: (12个空格) raise ValueError('invalid score') (4个空格) self.__score = score
    查看全部
    0 采集 收起 来源:python中 @property

    2018-03-22

  • Python 默认定义了__str__()和__repr__()两种方法,__str__()用于显示给用户,而__repr__()用于显示给开发人员。可以通过重写__str__来实现自定义 可以使__repr__和__repr__返回相同的内容:__repr__ = __str__
    查看全部
  • **kw是关键字参数,实质是一个dict,可以通过iteritems()进行迭代来读取key和value
    查看全部

举报

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

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