栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【原创】Python类属性操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【原创】Python类属性操作

简单类的定义
#定义个类,使用camel写法
class Sqlite3Apis:
	# 私有变量
    base_address = 0
    pointer_length = 4

	#定义一个(读)属性,外部可以通过obj.sqlite3_aggregate_context来访问
    @property
    def sqlite3_aggregate_context(self):
        return self.base_address + 0 * self.pointer_length
	#定义一个(写)属性,外部可以通过obj.sqlite3_aggregate_context=123来修改
    @sqlite3_aggregate_context.setter
    def sqlite3_aggregate_context(self, value):
        self.base_address = value - self.sqlite3_aggregate_context
类的使用
# 对上面的类进行实例化
aa = Sqlite3Apis()

# 对sqlite3_aggregate_context属性进行赋值
aa.sqlite3_aggregate_context = 0x12e21c0

#对 sqlite3_aggregate_context 属性进行访问
print(aa.sqlite3_aggregate_context)

# 遍历属性
for attr in dir(aa):
	# 排除类内置的属性
    if attr.startswith('__'):
        continue
    # 获取属性值,并以十六进制显示
    print(attr, hex(getattr(aa, attr)))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/846404.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号