python基础(7)

一、面向对象高级语法

1.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("chaoren")
d.eat() #报错

下面是报错信息:

Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/python/day7/静态方法.py", line 8, in <module>
    d.eat() #报错
TypeError: eat() missing 1 required positional argument: 'self'

调用报错的信息意思是eat需要一个self参数,但调用时却没有传递。当eat变成静态方法后,再通过实例调用时就不会自动把实例本身当作一个参数传递给self了。
如何正确运行呢?

两种方法:

第一种方法:调用时主动传递实例本身给eat方法,即d.eat(d),代码如下:

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

输出结果:
chaoren is eating

1.2 类方法:

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

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

执行报错结果:

Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/python/day7/类方法.py", line 8, in <module>
    d.eat()
  File "C:/Users/admin/PycharmProjects/python/day7/类方法.py", line 6, in eat
    print ("%s is eating" % self.name)
AttributeError: type object 'Dog' has no attribute 'name'

上面的报错是说Dog类没有name属性,因为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("chaoren")
d.eat()

执行结果:
大家 is eating

1.3 属性方法

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

class Dog(object):
    def __init__(self,name):
        self.name = name
    @property
    def eat(self):
        print("%s is eating" % self.name)
d = Dog("chaoren")
d.eat  #这里不应该是d.eat(),如果用d.eat()调用就会报错说NoneType is not callable, 因为eat此时已经变成一个静态属性了, 不是方法了, 想调用已经不需要加()号了,直接d.eat就可以了

输出结果:
chaoren is eating

1.4 类的特殊成员方法

class A(object):
    pass
a = A()
#1. __doc__表示类的描述信息
print(A.__doc__)
#2.__module__表示当前操作的对象在哪个模块
print(A.__module__)
print(a.__module__)
#3.__class__表示当前操作的对象的类是什么
print(a.__class__)
#4.__init__构造方法,通过类创建对象时,自动触发执行
#5.__del__西沟方法,当对象在内存中被释放时,自动触发执行
#6.__call__实例对象后面加括号直接执行
#析构方法的执行时由创建对象触发的,即:对象=类名(),而对于__call__方法的执行是由对象加后括号触发的,即:对象()或者类()()

#7.__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__)
obj1 = Province('Hebei',20000)
#获取对象obj1的成员
print (obj1.__dict__)

输出结果:
{'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Province' objects>, 'country': 'China', '__init__': <function Province.__init__ at 0x000000000103D840>, 'func': <function Province.func at 0x000000000103D8C8>, '__doc__': None, '__weakref__': <attribute '__weakref__' of 'Province' objects>}
{'count': 20000, 'name': 'Hebei'}

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

class Foo:
    def __str__(self):
        return  'This is string'
obj = Foo()
print(obj)

输出结果:
This is string

#9.__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__

输出结果:

__getitem__ k1
__setitem__ k2 lin
__delitem__ k1


二、反射

通过字符串映射或修改程序运行时的状态、属性、方法。

python中的反射功能是由以下四个内置函数提供:hasattr、getattr、setattr、delattr,改四个函数分别用于对对象内部执行:检查是否含有某成员、获取成员、设置成员、删除成员。

def dulk():
    print("%s is ...")
class Dog(object):
     def __init__(self,name):
         self.name = name
     def eat(self):
         print("%s is eating..." %self.name)
d = Dog("niuhanyang")
choice = input(">>:").strip()    #输入的是Dog这个类里面的方法---->eat
if hasattr(d,choice):            #判断choice(输入的)字符串在d(类)里面是否存在
    #print(getattr(d,choice))      #获取方法并打印
    getattr(d,choice)()           #获取方法并执行
else:
    setattr(d,choice,dulk)            #d.choice = dulk的内存地址
    fun = getattr(d,choice)
    fun()

输出结果:
>>:eat
niuhanyang is eating...

三、异常处理

1)常用异常:

AttributeError 试图访问一个对象没有的属性,比如foo.x,但是foo没有属性x

IOError 输入/输出异常;基本上是无法打开文件

ImportError 无法引入模块或包;基本上是路径问题或名称错误
IndentationError 语法错误(的子类) ;代码没有正确对齐
IndexError 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5]
KeyError 试图访问字典里不存在的键
KeyboardInterrupt Ctrl+C被按下
NameError 使用一个还未被赋予对象的变量
SyntaxError Python代码非法,代码不能编译(个人认为这是语法错误,写错了)
TypeError 传入对象类型与要求的不符合
UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量,
导致你以为正在访问它
ValueError 传入一个调用者不期望的值,即使值的类型是正确的
2)更多异常:
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
3)异常实现方式
s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)

#上面这种形式判断形式太单一,如果没有捕获到异常,程序就直接出错了。

出错信息如下:

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/python/day7/try.py", line 3, in <module>
    int(s1)
ValueError: invalid literal for int() with base 10: 'hello'

#考虑多种情况的代码如下:

s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)
except KeyError as e:
    print(e)
except ValueError as e:
    print(e)

显示结果如下:

invalid literal for int() with base 10: 'hello'

#但是我们可能还是有未考虑到的,所以要写个万能捕捉异常:

s1 = 'hello'
try:
    int(s1)
except Exception as e:
    print(e)
#异常其他结构
try:
    # 主代码块
    pass
except KeyError as e:
    # 异常时,执行该块
    pass
else:
    # 主代码块执行完,执行该块
    pass
finally:
    # 无论异常与否,最终执行该块
    pass

#主动触发异常
try:
    raise Exception('错误了。。。')
except Exception as e:
    print(e)
 #自定义异常
class alleyError(Exception):
    def __init__(self, msg):
        self.message = msg    
try :
    raise alleyError('数据库连接错误')
except alleyError 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)


 

 

posted @ 2016-09-09 12:28  chaishao  阅读(246)  评论(0编辑  收藏  举报