介绍Python设置属性值方法setattr()和判断变量是否存在的方法
以属性名为变量的方式给一个对象添加属性
使用 setattr 方法
obj = SomeClass()
key = 'key_name'
val = 'key_value'
setattr(obj, key, val)
print(obj.key_name)
示例:给报文字段赋值
from scapy.all import *
layer = {"ether":"dst='33:33:00:01:00:02',src='00:0c:29:d9:98:c7'"}
Ethernet_field = layer.setdefault('ether')
ethernet = Ether(type=0x86dd)
fields = dict(((lambda a:(a[0].strip("'"),a[1].strip("'"))) (field.split('=')) for field in Ethernet_field.split(',')))
for key, val in fields.items():
setattr(ethernet, key, val)
ethernet.show()
out:
>>> ethernet.show()
###[ Ethernet ]###
dst = 33:33:00:01:00:02
src = 00:0c:29:d9:98:c7
type = IPv6
判断变量是否存在
三种方法:
- locals().keys()
- dir()
- vars()
ethernet = 'Ether'
dhcpv6 = 'dhcpv6_solicit'
message = ['ethernet','ip','udp','dhcpv6']
del_message = []
for layer_i, value in enumerate(message):
if message[layer_i] not in locals().keys():
del_message.append(value)
for m in del_message:
message.remove(m)
print(f'message:{message}')
运行结果:
C:\Users\DELL>python3 test_locals.py
message:['ethernet', 'dhcpv6']
文章标题:Python笔记:属性值设置和判断变量是否存在
本文作者:hiyo
本文链接:https://www.cnblogs.com/hiyong/p/14405425.html
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!