Python-Day7 面向对象进阶/异常处理/Socket

1、面向对象高级语法部分python

1.静态方法

    经过@staticmethod装饰器便可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,能够在实例化后直接调用,而且在方法里能够经过self.调用实例变量或类变量,但静态方法是不能够访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实至关于跟类自己已经没什么关系了,它与类惟一的关联就是须要经过类名来调用这个方法。程序员

class Dog(object): def __init__(self,name): self.name = name @staticmethod #把eat方法变为静态方法
    def eat(self): print("%s is eating" % self.name) d = Dog("ha") d.eat()

上面的调用会出如下错误,说是eat须要一个self参数,但调用时却没有传递,没错,当eat变成静态方法后,再经过实例调用时就不会自动把实例自己看成一个参数传给self了。编程

<span style="color: #ff0000;">Traceback (most recent call last): File "/Users/jieli/PycharmProjects/python基础/自动化day7面向对象高级/静态方法.py", line 17, in <module> d.eat() TypeError: eat() missing 1 required positional argument: 'self'
</span>

想让上面的代码能够正常工做有两种办法ide

1. 调用时主动传递实例自己给eat方法,即d.eat(d) 函数

2. 在eat方法中去掉self参数,但这也意味着,在eat中不能经过self.调用实例中的其它变量了ui

class Dog(object): def __init__(self,name): self.name = name @staticmethod def eat(): print(" is eating") d = Dog("ha") d.eat()

2.类方法

    类方法经过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量。spa

class Dog(object): def __init__(self,name): self.name = name @classmethod def eat(self): print("%s is eating" % self.name) d = Dog("ha") d.eat()

执行报错以下,说Dog没有name属性,由于name是个实例变量,类方法是不能访问实例变量的code

Traceback (most recent call last): File "/Users/jieli/PycharmProjects/python基础/自动化day7面向对象高级/类方法.py", line 16, in <module> d.eat() File "/Users/jieli/PycharmProjects/python基础/自动化day7面向对象高级/类方法.py", line 11, in eat print("%s is eating" % self.name) AttributeError: type object 'Dog' has no attribute 'name'

此时能够定义一个类变量,也叫name,看下执行效果对象

class Dog(object): name = "我是类变量"
    def __init__(self,name): self.name = name @classmethod def eat(self): print("%s is eating" % self.name) d = Dog("ha") d.eat() #执行结果
 我是类变量 is eating

3.属性方法

属性方法的做用就是经过@property把一个方法变成一个静态属性blog

class Dog(object): def __init__(self,name): self.name = name @property def eat(self): print(" %s is eating" %self.name) d = Dog("ha") d.eat()

调用会出如下错误, 说NoneType is not callable, 由于eat此时已经变成一个静态属性了, 不是方法了, 想调用已经不须要加()号了,直接d.eat就能够了

Traceback (most recent call last): ChenRonghua is eating File "/Users/jieli/PycharmProjects/python基础/自动化day7面向对象高级/属性方法.py", line 16, in <module> d.eat() TypeError: 'NoneType' object is not callable

正常调用以下

d = Dog("ha") d.eat 输出 ha is eating

4.类的特殊成员方法

 __doc__  表示类的描述信息

class Foo: """ 描述类信息,这是用于看片的神奇 """
 
    def func(self): pass
 
print Foo.__doc__
#输出:类的描述信息

__module__ 和  __class__

__module__ 表示当前操做的对象在那个模块

__class__     表示当前操做的对象的类是什么

class C: def __init__(self): self.name = 'wupeiqi'
from lib.aa import C obj = C() print obj.__module__  # 输出 lib.aa,即:输出模块
print obj.__class__      # 输出 lib.aa.C,即:输出类

__init__ 构造方法,经过类建立对象时,自动触发执行

__del__ 析构方法,当对象在内存中被释放时,自动触发执行

注:此方法通常无须定义,由于Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,由于此工做都是交给Python解释器来执行,因此,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的。

__call__ 对象后面加括号,触发执行


  
注:构造方法的执行是由建立对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()

__dict__ 查看类或对象中的全部成员

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__
# 获取 对象obj1 的成员 # 输出:{'count': 10000, 'name': 'HeBei'}
 obj2 = Province('HeNan', 3888) print obj2.__dict__
# 获取 对象obj1 的成员 # 输出:{'count': 3888, 'name': 'HeNan'}

__str__ 若是一个类中定义了__str__方法,那么在打印 对象 时,默认输出该方法的返回值。

class Foo: def __str__(self): return 'alex li' obj = Foo() print obj # 输出:alex li

__getitem__、__setitem__、__delitem__

用于索引操做,如字典。以上分别表示获取、设置、删除数据

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'] = 'alex'   # 自动触发执行 __setitem__
del obj['k1']   

__new__ \ __metaclass__

class Foo(object): def __init__(self,name): self.name = name f = Foo("alex")

上述代码中,obj 是经过 Foo 类实例化的对象,其实,不只 obj 是一个对象,Foo类自己也是一个对象,由于在Python中一切事物都是对象

若是按照一切事物都是对象的理论:obj对象是经过执行Foo类的构造方法建立,那么Foo类对象应该也是经过执行某个类的 构造方法 建立。

print type(f) # 输出:<class '__main__.Foo'> 表示,obj 对象由Foo类建立
print type(Foo) # 输出:<type 'type'> 表示,Foo类对象由 type 类建立

因此,f对象是Foo类的一个实例Foo类对象是 type 类的一个实例,即:Foo类对象 是经过type类的构造方法建立。

那么,建立类就能够有两种方式:

a). 普通方式 

class Foo(object): def func(self): print 'hello alex'

b). 特殊方式

def func(self): print 'hello wupeiqi' Foo = type('Foo',(object,), {'func': func}) #type第一个参数:类名 #type第二个参数:当前类的基类 #type第三个参数:类的成员
def func(self): print("hello %s"%self.name) def __init__(self,name,age): self.name = name self.age = age Foo = type('Foo',(object,),{'func':func,'__init__':__init__}) f = Foo("jack",22) f.func()

类 是由 type 类实例化产生

那么问题来了,类默认是由 type 类实例化产生,type类中如何实现的建立类?类又是如何建立对象?

答:类中有一个属性 __metaclass__,其用来表示该类由 谁 来实例化建立,因此,咱们能够为 __metaclass__ 设置一个type类的派生类,从而查看 类 建立的过程。

#_*_coding:utf-8_*_
__author__ = 'Alex Li'
 
 
class MyType(type): def __init__(self, what, bases=None, dict=None): print("--MyType init---") super(MyType, self).__init__(what, bases, dict) def __call__(self, *args, **kwargs): print("--MyType call---") obj = self.__new__(self, *args, **kwargs) self.__init__(obj, *args, **kwargs) class Foo(object): __metaclass__ = MyType def __init__(self, name): self.name = name print("Foo ---init__") def __new__(cls, *args, **kwargs): print("Foo --new--") return object.__new__(cls) # 第一阶段:解释器从上到下执行代码建立Foo类 # 第二阶段:经过Foo类建立obj对象
obj = Foo("Alex")

5、反射

经过字符串映射或修改程序运行时的状态、属性、方法, 有如下4个方法

① hasattr(obj,str) 判断一个对象obj里是否有对应的str字符串的方法

② getattr(obj,str) 根据字符串去获取obj对象里的对应的方法的内存地址

#hasattr、getattr
 
class Foo(object): def __init__(self,name): self.name = name def func(self): print("func",self.name) obj = Foo("alex") str = "func"
 
print(hasattr(obj,str))   # 检查是否含有成员 有没有obj.str属性
 
if hasattr(obj,str): getattr(obj,str)() #getattr(obj,str) = obj.str
 
# True # func alex

③ setattr(obj,'y','z')  obj.y = z

#setattr
 
def bulk(self): print("%s is yelling"%self.name) class Foo(object): def __init__(self,name): self.name = name def func(self): print("func",self.name) obj = Foo("alex") str = "talk"
 
print(hasattr(obj,str))   # 检查是否含有成员 有没有obj.str属性
 
if hasattr(obj,str): getattr(obj,str)() # getattr(obj,str) = obj.str
 
else: setattr(obj,str,bulk) # setattr(obj,str,bulk 至关于 obj.str = bulk
 getattr(obj,str)(obj) # False # alex is yelling

④ delattr(obj,str) 删除obj.str

#delattr
 
class Foo(object): def __init__(self,name): self.name = name def func(self): print("func",self.name) obj = Foo("alex") str = "name"
 
if hasattr(obj,str): delattr(obj,str) # 删除属性obj.str
 
print(obj.name) # Traceback (most recent call last): # File "C:/Users/L/PycharmProjects/s14/preview/Day7/main.py", line 40, in <module> # print(obj.name) # AttributeError: 'Foo' object has no attribute 'name'

2、异常处理

一、异常基础

在编程过程当中为了增长友好性,在程序出现bug时通常不会将错误信息显示给用户,而是现实一个提示的页面,通俗来讲就是不让用户看见大黄页!!!

#异常处理
 list = ["hello","world"] try: list[3] except IndexError as e: print("IndexError",e) # IndexError list index out of range

上面程序表示try里面的代码若是出现IndexError这种错误,则执行except下面的代码,不会把错误信息显示给用户,程序也不会中止;目前只能处理IndexError这一种异常,想处理多种异常还能够这么写:

#多种异常处理
 list = ["hello","world"] try: list[3] except IndexError as e: print("IndexError",e) except KeyError as e: print("KeyError", e) except ValueError as e: print("ValueError",e) # IndexError list index out of range

万能异常 在python的异常中,有一个万能异常:Exception,他能够捕获任意异常

#万能异常处理
 list = ["hello","world"] try: list[3] except Exception as e: print("Error",e) #Error list index out of range
AttributeError 试图访问一个对象没有的树形,好比foo.x,可是foo没有属性x IOError 输入/输出异常;基本上是没法打开文件 ImportError 没法引入模块或包;基本上是路径问题或名称错误 IndentationError 语法错误(的子类) ;代码没有正确对齐 IndexError 下标索引超出序列边界,好比当x只有三个元素,却试图访问x[5] KeyError 试图访问字典里不存在的键 KeyboardInterrupt Ctrl+C被按下 NameError 使用一个还未被赋予对象的变量 SyntaxError Python代码非法,代码不能编译(我的认为这是语法错误,写错了) TypeError 传入对象类型与要求的不符合 UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是因为另有一个同名的全局变量, 致使你觉得正在访问它 ValueError 传入一个调用者不指望的值,即便值的类型是正确的
ArithmeticError
AssertionError
AttributeError
BaseException
BufferError
BytesWarning
DeprecationWarning
EnvironmentError
EOFError
Exception
FloatingPointError
FutureWarning
GeneratorExit
ImportError
ImportWarning
IndentationError
IndexError
IOError
KeyboardInterrupt
KeyError
LookupError
MemoryError
NameError
NotImplementedError
OSError
OverflowError
PendingDeprecationWarning
ReferenceError
RuntimeError
RuntimeWarning
StandardError
StopIteration
SyntaxError
SyntaxWarning
SystemError
SystemExit
TabError
TypeError
UnboundLocalError
UnicodeDecodeError
UnicodeEncodeError
UnicodeError
UnicodeTranslateError
UnicodeWarning
UserWarning
ValueError
Warning
ZeroDivisionError
所有异常

二、异常的其余结构

#异常的其余结构
 
try: # 主代码块
    pass
except KeyError as e: # 异常时,执行该块
    pass
else: # 主代码块执行完,执行该块
    pass
finally: # 不管异常与否,最终执行该块
    pass

三、自定义异常 

#主动触发异常
 
try: raise Exception('错误了。。。') except Exception as e: print(e) #错误了。。。
#自定义异常
 
class Diyexception(Exception): def __init__(self, msg): self.message = msg def __str__(self): return self.message error = Diyexception("报错了....") try: raise error except Diyexception as e: print(e) #报错了....
相关文章
相关标签/搜索