温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中映射是什么

发布时间:2020-08-05 15:32:04 来源:亿速云 阅读:240 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关Python中映射是什么的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

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

获取成员: 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)

运行结果:

>>>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)

运行结果:

<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'))

运行结果:

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)

运行结果:

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)

运行结果:

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)

运行结果:

666

创建两个文件,s1.py 和 s2.py

s2.py 内容如下:

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

s1.py 内容如下:

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

运行 s1.py 文件:

klvchen
func

创建两个文件,s1.py 和 s2.py

s2.py 内容如下:

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

s1.py 内容如下:

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 文件,运行结果:

klvchen
cwe
<class 's2.Foo'>
<s2.Foo object at 0x000001CFCDBB2438>
666

创建两个文件,s1.py 和 s2.py

s2.py 内容如下:

def f1():
    return '首页'
def f2():
    return '新闻'
def f3():
    return '精华'

s1.py 内容如下:

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

运行 s1.py 文件,运行结果:

请输入要查看的URL: f1
首页

感谢各位的阅读!关于Python中映射是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI