Python Day7

概述

  • 面向对象高级语法部分

    静态方法、类方法、属性方法

    类的特殊方法

    反射

静态方法

通过@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("wangcai")
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>

想让上面的代码可以正常工作有两种办法

  1. 调用时主动传递实例本身给eat方法,即d.eat(d)

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

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

类方法

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

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

执行报错如下,说Dog没有name属性,因为name是个实例变量,类方法是不能访问实例变量的

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("wangcai")
d.eat()
 
 
#执行结果
 
我是类变量 is eating

属性方法

属性方法的作用就是通过@property把一个方法变成一个静态属性

class Dog(object):
 
    def __init__(self,name):
        self.name = name
 
    @property
    def eat(self):
        print(" %s is eating" %self.name)
 
 
d = Dog("wangcai")
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("wangcai")
d.eat
 
 # 输出    
 wangcai is eating
由属性的定义和调用要注意一下几点:
  • 定义时,在普通方法的基础上添加 @property 装饰器;
  • 定义时,属性仅有一个self参数
  • 调用时,无需括号

实例:对于主机列表页面,每次请求不可能把数据库中的所有内容都显示到页面上,而是通过分页的功能局部显示,所以在向数据库中请求数据时就要显示的指定获取从第m条到第n条的所有数据(即:limit m,n),这个分页的功能包括:

  • 根据用户请求的当前页和总数据条数计算出 m 和 n
  • 根据m 和 n 去数据库中请求数据
# ############### 定义 ###############
class Pager:
    
    def __init__(self, current_page):
        # 用户当前请求的页码(第一页、第二页...)
        self.current_page = current_page
        # 每页默认显示10条数据
        self.per_items = 10 


    @property
    def start(self):
        val = (self.current_page - 1) * self.per_items
        return val

    @property
    def end(self):
        val = self.current_page * self.per_items
        return val

# ############### 调用 ###############

p = Pager(1)
p.start #就是起始值,即:m
p.end   #就是结束值,即:n
我们知道Python中的类有经典类和新式类,新式类的属性比经典类的属性丰富。( 如果类继object,那么该类是新式类 )

经典类,具有一种@property装饰器(如上一步实例)

# ############### 定义 ###############    
class Goods:

    @property
    def price(self):
        print('123')
# ############### 调用 ###############
obj = Goods()
obj.price  # 自动执行 @property 修饰的 price 方法

新式类,具有三种@property装饰器

# ############### 定义 ###############
class Goods(object):

    @property
    def price(self):
        print ('@property')

    @price.setter
    def price(self, value):
        print ('@price.setter')

    @price.deleter
    def price(self):
        print ('@price.deleter')

# ############### 调用 ###############
obj = Goods()

obj.price          # 自动执行 @property 修饰的 price 方法,并获取方法的返回值

obj.price = 123    # 自动执行 @price.setter 修饰的 price 方法,并将  123 赋值给方法的参数

del obj.price      # 自动执行 @price.deleter 修饰的 price 方法
注:经典类中的属性只有一种访问方式,其对应被 @property 修饰的方法
新式类中的属性有三种访问方式,并分别对应了三个被@property、@方法名.setter、@方法名.deleter修饰的方法

由于新式类中具有三种访问方式,我们可以根据他们几个属性的访问特点,分别将三个方法定义为对同一个属性:获取、修改、删除

#########实例############

class Goods(object):

    def __init__(self):
        # 原价
        self.original_price = 100
        # 折扣
        self.discount = 0.8

    @property
    def price(self):
        # 实际价格 = 原价 * 折扣
        new_price = self.original_price * self.discount
        return new_price

    @price.setter
    def price(self, value):
        self.original_price = value

    @price.deltter
    def price(self, value):
        del self.original_price

obj = Goods()
obj.price         # 获取商品价格
obj.price = 200   # 修改商品原价
del obj.price     # 删除商品原价

类的特殊成员方法

1.__doc__

表示类的描述信息

class Foo:
    """ 类的描述信息 """

    def func(self):
        pass

print Foo.__doc__
#输出:类的描述信息
2.__module__ 和 __class__

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

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

class C:

    def __init__(self):
        self.name = 'zhangsan'
from lib.aa import C

obj = C()
print (obj.__module__) # 输出 lib.aa,即:输出模块
print (obj.__class__)      # 输出 lib.aa.C,即:输出类
3.__call__ 对象后面加括号,触发执行。
注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 call 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()
class Foo:
 
    def __init__(self):
        pass
     
    def __call__(self, *args, **kwargs):
 
        print ('__call__')
 
 
obj = Foo() # 执行 __init__
obj()       # 执行 __call__
4.__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'}
5.__str__ 如果一个类中定义了__str__方法,那么在打印 对象 时,默认输出该方法的返回值。
class Foo:
 
    def __str__(self):
        return 'Shao Lin'
 
 
obj = Foo()
print (obj)
# 输出:Shao Lin
6.__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'] = 'lin'   # 自动触发执行 __setitem__
del obj['k1']   # 自动触发执行 __delitem__

其他相关

一、isinstance(obj, cls)

检查是否obj是否是类 cls 的对象

class Foo(object):
    pass
 
obj = Foo()
 
isinstance(obj, Foo)

二、issubclass(sub, super)

检查sub类是否是 super 类的派生类

class Foo(object):
    pass
 
class Bar(Foo):
    pass
 
issubclass(Bar, Foo)

反射

通过字符串映射或修改程序运行时的状态、属性、方法。
python中的反射功能是由以下四个内置函数提供:hasattr、getattr、setattr、delattr,改四个函数分别用于对对象内部执行:检查是否含有某成员、获取成员、设置成员、删除成员。
def bulk(self):
    print("%s is yelling...." %self.name)

class Dog(object):
    def __init__(self,name):
        self.name = name

    def eat(self,food):
        print("%s is eating..."%self.name,food)


d = Dog("wangcai")
choice = input(">>:").strip()

if hasattr(d,choice): # 查看是否有此成员
    #获取成员并调用
    func=getattr(d,choice) 
    func()
else:
    #设置成员并调用
    setattr(d,choice,bulk) #d.talk = bulk
    func = getattr(d, choice)
    func(d)
    
#删除成员
delatter(d,choice)

异常处理

在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面。

try:
    # 主代码块
    pass
except KeyError as e:
    # 异常时,执行该块
    pass
else:
    # 主代码块执行完没有异常,执行该块
    pass
finally:
    # 无论异常与否,最终执行该块
    pass
python中的异常种类非常多,每个异常专门用于处理某一项异常!!!
##########常用异常################
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

实例:IndexError

li = ["xiaoming", 'zhangsna']
try:
    dic[li]
except IndexError as e:
    print (e)

实例:KeyError

dic = {'k1':'v1'}
try:
    dic['k20']
except KeyError as e:
    print (e)

实例:ValueError

s1 = 'hello'
try:
    int(s1)
except ValueError as e:
    print (e)
万能异常 在python的异常中,有一个万能异常:Exception,他可以捕获任意异常
s1 = 'hello'
try:
    int(s1)
except Exception as e:
    print (e)
主动触发异常
try:
    raise Exception('错误了。。。')
except Exception as e:
    print (e)
自定义异常
class LinException(Exception):
 
    def __init__(self, msg):
        self.message = msg
 
    def __str__(self):
        return self.message
 
try:
    raise LinException('我的异常')
except LinException as e:
    print (e)
断言
# assert 条件
 
assert 1 == 1
 
assert 1 == 2
原文地址:https://www.cnblogs.com/shaolin2016/p/5852495.html