1、面向对象高级语法部分
1)静态方法、类方法、属性方法
3)类的特殊方法
4)反射
2、异常处理
通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Dog(object): def __init__(self,name): self.name = name @staticmethod #把eat方法变为静态方法 def eat(self): print("%s is eating" % self.name)d = Dog("ZhangSan")d.eat() |
上面的调用会出以下错误,说是eat需要一个self参数,但调用时却没有传递,当eat变成静态方法后,再通过实例调用时就不会自动把实例本身当作一个参数传给self了。
想让上面的代码可以正常工作有两种办法
1. 调用时主动传递实例本身给eat方法,即d.eat(d)
2. 在eat方法中去掉self参数,但这也意味着,在eat中不能通过self.调用实例中的其它变量了
1 class Dog(object):
2
3 def __init__(self,name):
4 self.name = name
5
6 @staticmethod
7 def eat():
8 print(" is eating")
9
10
11
12 d = Dog("ZhangSan")
13 d.eat()
类方法通过@classmethod装饰器实现,类方法和普通方法的区别是:类方法只能访问类变量,不能访问实例变量
|
1
2
3
4
5
6
7
8
9
10
11
12
|
class Dog(object): def __init__(self,name): self.name = name @classmethod def eat(self): print("%s is eating" % self.name)d = Dog(")d.eat() |
执行报错如下,说Dog没有name属性,因为name是个实例变量,类方法是不能访问实例变量的
|
1
2
3
4
5
6
|
Traceback (most recent call last): File "/Users/Gavin/PycharmProjects/python/类方法.py", line 16, in <module> d.eat() File "/Users/jieli/PycharmProjects/python/类方法.py", line 11, in eat print("%s is eating" % self.name)AttributeError: type object ‘Dog‘ has no attribute ‘name‘ |
此时可以定义一个类变量,也叫name,并执行:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Dog(object): name = "我是类变量" def __init__(self,name): self.name = name @classmethod def eat(self): print("%s is eating" % self.name)d = Dog(ZhangSan")d.eat()#执行结果我是类变量 is eating |
属性方法的作用就是通过@property把类的一个方法变成一个静态属性
|
1
2
3
4
5
6
7
8
9
10
11
12
|
class Dog(object): def __init__(self,name): self.name = name @property def eat(self): print(" %s is eating" %self.name)d = Dog(")d.eat() |
调用会出以下错误, 说NoneType is not callable, 因为eat此时已经变成一个静态属性了, 不是方法了, 想调用已经不需要加()号了,直接d.eat就可以了
|
1
2
3
4
5
|
Traceback (most recent call last): ChenRonghua is eating File "/Users/Gavin/PycharmProjects/python/属性方法.py", line 16, in <module> d.eat()TypeError: ‘NoneType‘ object is not callable |
正常调用如下
|
1
2
3
4
5
|
d = Dog(ZhangSan")d.eat输出 is eating |
|
1
2
3
4
5
6
7
8
|
class Foo: """ 描述类信息 """ def func(self): passprint(Foo.__doc__)#输出:描述类信息 |
__module__ 表示当前操作的对象在那个模块
__class__ 表示当前操作的对象的类是什么
新建一个文件lib.aa.py,内容如下
class C: def __init__(self): self.name = ‘Simons‘
在另一个文件下运行:
from lib.aa import C obj = C() print obj.__module__ # 输出 lib.aa,即:输出模块 print obj.__class__ # 输出 lib.aa.C,即:输出类
析构方法,当对象在内存中被释放时,自动触发执行。
注:此方法一般无须定义,因为Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的
5. __call__ 方法,在实例化的对象后面加括号,触发执行。
注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()
|
1
2
3
4
5
6
7
8
9
10
11
12
|
class Foo: def __init__(self): pass def __call__(self, *args, **kwargs): print ‘__call__‘obj = Foo() # 执行 __init__obj() # 执行 __call__ |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class Province: country = ‘China‘ def __init__(self, name, count): self.name = name self.count = count def func(self, *args, **kwargs): print ‘func‘# 打印所有类属性,【注意】:不包括实例属性print Province.__dict__# 输出:{‘country‘: ‘China‘, ‘__module__‘: ‘__main__‘, ‘func‘: <function func at 0x10be30f50>, ‘__init__‘: <function __init__ at 0x10be30ed8>, ‘__doc__‘: None}obj1 = Province(‘HeBei‘,10000)print obj1.__dict__# 打印所有实例属性,【注意】:但不包括类属性# 输出:{‘count‘: 10000, ‘name‘: ‘HeBei‘}obj2 = Province(‘HeNan‘, 3888)print obj2.__dict__# 获取 对象obj1 的成员,不包括类属性# 输出:{‘count‘: 3888, ‘name‘: ‘HeNan‘} |
|
1
2
3
4
5
6
7
8
9
|
class Foo: def __str__(self): return ‘Gavin Simons‘obj = Foo()print obj# 输出:Gavin Simons |
用于索引操作,如字典。以上分别表示获取、设置、删除数据
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Foo(object): def __getitem__(self, key): print(‘__getitem__‘,key) def __setitem__(self, key, value): print(‘__setitem__‘,key,value) def __delitem__(self, key): print(‘__delitem__‘,key)obj = Foo()result = obj[‘k1‘] # 自动触发执行 __getitem__obj[‘k2‘] = ‘gavin‘ # 自动触发执行 __setitem__del obj[‘k1‘] |
反射的作用就是,把字符串反射成内存中的对象或地址,进行来回的调用。这个功能很重要,因为这里实现了动态的内存装配。
通过字符串映射或修改程序运行时的状态、属性、方法, 有以下4个方法
getattr(object, name, default=None) #如果name是方法名,则返回方法的内存地址,如果是属性则返回具体数值
def getattr(object, name, default=None): # known special case of getattr
"""
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, ‘y‘) is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn‘t
exist; without it, an exception is raised in that case.
"""
pass
hasattr(object,name) #判断object中有没有一个name字符串对应的方法或属性
setattr(x, y, v) #除了可以设置类的方法,还可以设置类的属性
def setattr(x, y, v): # real signature unknown; restored from __doc__
"""
Sets the named attribute on the given object to the specified value.
setattr(x, ‘y‘, v) is equivalent to ``x.y = v‘‘
delattr(x, y)
def delattr(x, y): # real signature unknown; restored from __doc__
"""
Deletes the named attribute from the given object.
delattr(x, ‘y‘) is equivalent to ``del x.y‘‘
"""
反射代码示例
class Foo(object):
def __init__(self):
self.name = ‘wupeiqi‘
def func(self):
return ‘func‘
obj = Foo()
# #### 检查是否含有成员 ####
hasattr(obj, ‘name‘)
hasattr(obj, ‘func‘)
# #### 获取成员 ####
getattr(obj, ‘name‘)
getattr(obj, ‘func‘)
# #### 设置成员 ####
setattr(obj, ‘age‘, 18)
setattr(obj, ‘show‘, lambda num: num + 1)
# #### 删除成员 ####
delattr(obj, ‘name‘)
delattr(obj, ‘func‘)
参考 http://www.cnblogs.com/wupeiqi/articles/5017742.html
原文:http://www.cnblogs.com/GavinSimons/p/7775011.html