溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中映射是什么

發(fā)布時間:2020-08-05 15:32:04 來源:億速云 閱讀:258 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)Python中映射是什么的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

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

獲取成員: 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)

運(yùn)行結(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)

運(yùn)行結(jié)果:

<bound method Foo.show of <__main__.Foo object at 0x00000234F6942588>>
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'))

運(yùn)行結(jié)果:

False

設(shè)置成員: 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)

運(yùn)行結(jié)果:

value

刪除成員: 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)

運(yùn)行結(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)

運(yùn)行結(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)

運(yùn)行 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)

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

klvchen
cwe
<class 's2.Foo'>
<s2.Foo object at 0x000001CFCDBB2438>
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')

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

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

感謝各位的閱讀!關(guān)于Python中映射是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI