python的模块相当于java中的包
import 包名.模块名
import 包名.模块名 as 新模块名
forom 包名 import 模块名
forom 包名 import 模块名 as 新模块名
在python2.7/2.6
提供了json模块,但是python2.5
没有提供,这时候要想使用json模块就需要try...except...
确保导入模块,类似于java
的try ... catch...
旧版本想使用新的版本才有的功能可以使用该方法
例如:
在Python 3.x中,字符串统一为unicode,不需要加前缀 u,而以字节存储的str则必须加前缀 b。请利用future的unicode_literals在Python 2.7中编写unicode字符串
from __future__ import unicode_literalss = 'am I an unicode?'print isinstance(s, unicode)pip install [第三方模块名]
安装成功之后导入即刻import [第三方模块名]
注意__init__(self,**kw)
中的self
相当于java
的this
输出:
(Person:bart man)(Student:lisa women 98)Student...do()...(Person:maggie women)当给对象赋值时候
class Student(object): def __init__(self, name, score): self.name = name self.score = score当我们想要修改一个 Student
的 scroe
属性时,可以这么写:
s = Student('Bob', 59)
s.score = 60
但是也可以这么写:
s.score = 1000
显然,直接给属性赋值无法检查分数的有效性。
如果利用两个方法:
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('invalid score') self.__score = score这样一来,s.set_score(1000)
就会报错。
这种使用 get/set
方法来封装对一个属性的访问在许多面向对象编程的语言中都很常见。
但是写 s.get_score()
和 s.set_score()
没有直接写 s.score
来得直接。
有没有两全其美的方法?—-有。
因为Python支持高阶函数,在函数式编程中我们介绍了装饰器函数,可以用装饰器函数把 get/set 方法“装饰”成属性调用:
class Student(object): def __init__(self, name, score): self.name = name self.__score = score @property def score(self): return self.__score @score.setter def score(self, score): if score < 0 or score > 100: raise ValueError('invalid score') self.__score = scores = Student('Bob', 59)s.score = 60print s.scores.score = 1000print s.score注意: 第一个score(self)是get方法,用@property装饰,第二个score(self, score)是set方法,用@score.setter装饰,@score.setter是前一个@property装饰后的副产品
现在,就可以像使用属性一样设置score了:
>>> s = Student('Bob', 59)>>> s.score = 60>>> print s.score60>>> s.score = 1000Traceback (most recent call last): ...ValueError: invalid score说明对 score 赋值实际调用的是 set方法。
由于Python是动态语言,任何实例在运行期都可以动态地添加属性。 如果要限制添加的属性,例如,Student类
只允许添加 name
、gender
和score
这3个属性,就可以利用Python的一个特殊的__slots__
来实现。
顾名思义,_slots_是指一个类允许的属性列表:
class Student(object): __slots__ = ('name', 'gender', 'score') def __init__(self, name, gender, score): self.name = name self.gender = gender self.score = score现在,对实例进行操作:
>>> s = Student('Bob', 'male', 59)>>> s.name = 'Tim' # OK>>> s.score = 99 # OK>>> s.grade = 'A'Traceback (most recent call last): ...AttributeError: 'Student' object has no attribute 'grade'__slots__
的目的是限制当前类所能拥有的属性,如果不需要添加任意动态的属性,使用__slots__
也能节省内存。
在Python中,函数其实是一个对象:
>>> f = abs>>> f.__name__'abs'>>> f(-123)123由于 f 可以被调用,所以,f 被称为可调用对象。
所有的函数都是可调用对象。
一个类实例也可以变成一个可调用对象,只需要实现一个特殊方法call()。
我们把 Person 类变成一个可调用对象:
class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender def __call__(self, friend): print 'My name is %s...' % self.name print 'My friend is %s...' % friend现在可以对 Person 实例直接调用:
>>> p = Person('Bob', 'male')>>> p('Tim')My name is Bob...My friend is Tim...单看 p(‘Tim’) 你无法确定 p 是一个函数还是一个类实例,所以,在Python中,函数也是对象,对象和函数的区别并不显著。
新闻热点
疑难解答