python-类相关知识点
发布日期:2021-11-05 07:52:12
浏览次数:21
分类:技术文章
本文共 783 字,大约阅读时间需要 2 分钟。
知识点一:hasattr类中是否含有某属性
转发:
hasattr(object, name)object -- 对象。name -- 字符串,属性名。return如果对象有该属性返回 True,否则返回 False。
class variable: x = 1 y = 'a' z = Truedd = variable() print(hasattr(dd, 'x'))print(hasattr(dd, 'y'))print(hasattr(dd, 'z'))print(hasattr(dd, 'no'))
输出:
TrueTrueTrueFalse
知识点二:类属性和对象属性
类中使用self定义的是对象属性,不使用self的是类属性,类属性可以使用 “类名.属性名” 调用
class a(object): def __init__(self): self.a = 1 self.b = '1' c = 2aa = a()print(aa.c) #对象属性,可以使用对象名调用,也可以使用类名调用print( a.c)print(aa.b) print( a.b) #错误
知识点三:vars返回对象的属性及属性值字典
class a(object): def __init__(self): self.a = 1 self.b = '1'aa = a()print(vars(aa))
输出:
{'a': 1, 'b': '1'}
知识点四:python中能够通过对象新建成员变量
class a(): def __init__(self): self.aa = 1A = a()A.bb = 2print(A.bb)
2
转载地址:https://blog.csdn.net/xingghaoyuxitong/article/details/114523277 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年03月27日 22时29分46秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【Leetcode刷题篇】leetcode141 环形链表II
2019-04-26
【Leetcode刷题篇】leetcode160 相交链表
2019-04-26
【Leetcode刷题篇】leetcode169 多数元素
2019-04-26
【Leetcode刷题篇】leetcode461 汉明距离
2019-04-26
【Leetcode刷题篇】leetcode204 计数质数
2019-04-26
【Leetcode刷题篇】leetcode70 爬楼梯
2019-04-26
【Leetcode刷题篇】leetcode739 每日温度
2019-04-26
【Leetcode刷题篇】leetcode121买卖股票的最佳时机
2019-04-26
【面试篇】Java多线程并发-Java关键字volatile详解
2019-04-26
【面试篇】Java的代理模式-静态代理和动态代理详解
2019-04-26
【面试篇】 Java对象拷贝(对象克隆 对象复制)
2019-04-26
【Leetcode刷题篇】leetcode64 最小路径和
2019-04-26
【Leetcode刷题篇】leetcode79 单词搜索
2019-04-26
【Leetcode刷题篇】leetcode300 最长上升子序列
2019-04-26
【Leetcode刷题篇】leetcode394 字符串解码
2019-04-26
【Leetcode刷题篇】leetcode152 乘积最大数组
2019-04-26
【Leetcode刷题篇】leetcode56 合并区间
2019-04-26
【Leetcode刷题篇】leetcode210 课程表II
2019-04-26
【Leetcode刷题篇】leetcode207 课程表
2019-04-26
【Leetcode刷题篇】leetcode322 零钱兑换
2019-04-26