您的位置:时时app平台注册网站 > 编程知识 > python之面向对象的高级进阶【时时app平台注册网

python之面向对象的高级进阶【时时app平台注册网

2019-10-30 04:16

一 isinstance(obj,cls)和issubclass(sub,super)

  isinstance(obj,cls)检查是还是不是obj是或不是是类 cls 的对象

1 class Foo(object):
2     pass
3  
4 obj = Foo()
5  
6 isinstance(obj, Foo)

  issubclass(sub, super)检查sub类是不是是 super 类的派生类

class Foo(object):
    pass

class Bar(Foo):
    pass

issubclass(Bar, Foo)

 

二、反射

2 python面向对象中的反射:通过字符串的款型操作对象相关的性质。python中的一切事物都以指标(都能够运用反射)

多少个能够兑现自省的函数

def getattr(object , name, defalut=None) :

判断object中有没有一个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

def 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''
    """
    pass

def 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''
    """
    pass

 

进行案例

class BlackMedium:
    feature='Ugly'
    def __init__(self,name,addr):
        self.name=name
        self.addr=addr

    def sell_house(self):
        print('%s 黑中介卖房子啦,傻逼才买呢,但是谁能证明自己不傻逼' %self.name)
    def rent_house(self):
        print('%s 黑中介租房子啦,傻逼才租呢' %self.name)

b1=BlackMedium('万成置地','回龙观天露园')

#检测是否含有某属性
print(hasattr(b1,'name'))
print(hasattr(b1,'sell_house'))

#获取属性
n=getattr(b1,'name')
print(n)
func=getattr(b1,'rent_house')
func()

# getattr(b1,'aaaaaaaa') #报错
print(getattr(b1,'aaaaaaaa','不存在啊'))  

#设置属性
setattr(b1,'sb',True)
setattr(b1,'show_name',lambda self:self.name 'sb')#给Blackhouser添加SB属性
print(b1.__dict__)
print(b1.show_name(b1))

#删除属性
delattr(b1,'addr')
delattr(b1,'show_name') 
delattr(b1,'show_name111')#不存在,则报错

print(b1.__dict__)

类也是对象
class Foo(object):

    staticField = "old boy"

    def __init__(self):
        self.name = 'wupeiqi'

    def func(self):
        return 'func'

    @staticmethod
    def bar():
        return 'bar'

print getattr(Foo, 'staticField')
print getattr(Foo, 'func')
print getattr(Foo, 'bar')



#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys


def s1():
    print 's1'


def s2():
    print 's2'


this_module = sys.modules[__name__]

hasattr(this_module, 's1')
getattr(this_module, 's2')

#!/usr/bin/env python
# -*- coding:utf-8 -*-

def test():
    print('from the test')

时时app平台注册网站 1时时app平台注册网站 2

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3  
 4 """
 5 程序目录:
 6     module_test.py
 7     index.py
 8  
 9 当前文件:
10     index.py
11 """
12 
13 import module_test as obj
14 
15 #obj.test()
16 
17 print(hasattr(obj,'test'))
18 
19 getattr(obj,'test')()

View Code

 

3 为啥用反射之反射的益处

 

收益黄金年代:实现可插拔机制

总的说来反射的好处是,能够预先定义好接口,接口独有在被成功后才会真的奉行,那落实了即插即用,那实际是大器晚成种‘中期绑定’,什么看头?即你能够优先把第生机勃勃的逻辑写好(只定义接口),然后前期再去达成接口的成效

 

class FtpClient:
    'ftp客户端,但是还么有实现具体的功能'
    def __init__(self,addr):
        print('正在连接服务器[%s]' 
	  

本文由时时app平台注册网站发布于编程知识,转载请注明出处:python之面向对象的高级进阶【时时app平台注册网

关键词: