日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:一文讀懂Python中的映射

python中的反射功能是由以下四個內(nèi)置函數(shù)提供:hasattr、getattr、setattr、delattr,改四個函數(shù)分別用于對對象內(nèi)部執(zhí)行:檢查是否含有某成員、獲取成員、設置成員、刪除成員。

創(chuàng)新互聯(lián)是一家以成都網(wǎng)站建設、網(wǎng)頁設計、品牌設計、軟件運維、seo優(yōu)化排名、小程序App開發(fā)等移動開發(fā)為一體互聯(lián)網(wǎng)公司。已累計為成都橡塑保溫等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務。

獲取成員: getattr

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age
obj = Foo('klvchen', 18)
inp = input('>>>')
v = getattr(obj, inp)
print(v)

運行結(jié)果:

>>>name
klvchen
class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def show(self):
        return "%s-%s" %(self.name, self.age)
obj = Foo('klvchen', 18)
func = getattr(obj, 'show')
print(func)
res = func()
print(res)

運行結(jié)果:

>
klvchen-18

檢查是否含有成員: hasattr

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def show(self):
        return "%s-%s" %(self.name, self.age)
obj = Foo('klvchen', 18)
print(hasattr(obj, 'name1'))

運行結(jié)果:

False

設置成員: setattr

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def show(self):
        return "%s-%s" %(self.name, self.age)
obj = Foo('klvchen', 18)
# print(hasattr(obj, 'name1'))
setattr(obj, 'key', 'value')
print(obj.key)

運行結(jié)果:

value

相關(guān)推薦:《Python視頻教程》

刪除成員: delattr

class Foo:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def show(self):
        return "%s-%s" %(self.name, self.age)
obj = Foo('klvchen', 18)
print(obj.name)
delattr(obj, 'name')
print(obj.name)

運行結(jié)果:

klvchen
AttributeError: 'Foo' object has no attribute 'name'

通過字符串的形式操作對象中的成員

class Foo:
    stat = '666'
    def __init__(self, name, age):
        self.name = name
        self.age = age
res = getattr(Foo, 'stat')
print(res)

運行結(jié)果:

666

創(chuàng)建兩個文件,s1.py 和 s2.py

s2.py 內(nèi)容如下:

NAME = 'klvchen'
def func():
    return 'func'

s1.py 內(nèi)容如下:

import s2
res1 = getattr(s2, 'NAME')
print(res1)
res2 = getattr(s2, 'func')
result = res2()
print(result)

運行 s1.py 文件:

klvchen
func

創(chuàng)建兩個文件,s1.py 和 s2.py

s2.py 內(nèi)容如下:

NAME = 'klvchen'
def func():
    return 'cwe'
class Foo:
    def __init__(self):
        self.name = 666

s1.py 內(nèi)容如下:

import s2
res1 = getattr(s2, 'NAME')
print(res1)
res2 = getattr(s2, 'func')
result = res2()
print(result)
cls = getattr(s2, 'Foo')
print(cls)
obj = cls()
print(obj)
print(obj.name)

運行 s1.py 文件,運行結(jié)果:

klvchen
cwe


666

創(chuàng)建兩個文件,s1.py 和 s2.py

s2.py 內(nèi)容如下:

def f1():
    return '首頁'
def f2():
    return '新聞'
def f3():
    return '精華'

s1.py 內(nèi)容如下:

import s2
inp = input('請輸入要查看的URL: ')
if hasattr(s2, inp):
    func = getattr(s2, inp)
    result = func()
    print(result)
else:
    print('404')

運行 s1.py 文件,運行結(jié)果:

請輸入要查看的URL: f1
首頁

本文名稱:創(chuàng)新互聯(lián)Python教程:一文讀懂Python中的映射
分享路徑:http://www.5511xx.com/article/cdiophe.html