博客
关于我
天池龙珠计划Python训练营——第八天
阅读量:215 次
发布时间:2019-02-28

本文共 3029 字,大约阅读时间需要 10 分钟。

??????????????????Python??????????????????????????????????????????????????????????????????????

?????

?Python????????????????????????????????__??????????????????????????????????????

?????????????JustCounter???__secretCount???????publicCount???????

class JustCounter:    __secretCount = 0  # ????    publicCount = 0    # ????    def count(self):        self.__secretCount += 1        self.publicCount += 1        print(self.__secretCount)

????counter = JustCounter()?????

  • count()?????????1
  • publicCount?????????2
  • ????__secretCount???AttributeError?????????

??????????????Python????????????????????????????????

  • print(counter._JustCounter__secretCount)????????
  • ?print(counter.__secretCount)??????

????

?Python???????BIF?Built-in Functions????????????????????????????????????????

1. issubclass()

issubclass(class, classinfo)????class???classinfo??????????????????classinfo????????????????????

???

class A:    passclass B(A):    passprint(issubclass(B, A))  # ??Trueprint(issubclass(B, B))  # ??Trueprint(issubclass(A, B))  # ??Falseprint(issubclass(B, object))  # ??True

2. isinstance()

isinstance(object, classinfo)?????????????????? inheritance chain??type()???isinstance()????????

???

a = 2print(isinstance(a, int))  # ??Trueprint(isinstance(a, str))  # ??Falseprint(isinstance(a, (str, int, list)))  # ??Trueclass A:    passclass B(A):    passprint(isinstance(A(), A))  # ??Trueprint(type(A()) == A)  # ??Trueprint(isinstance(B(), A))  # ??Trueprint(type(B()) == A)  # ??False

3. hasattr()

hasattr(object, name)????????????????

???

class Coordinate:    x = 10    y = -5    z = 0point1 = Coordinate()print(hasattr(point1, 'x'))  # ??Trueprint(hasattr(point1, 'y'))  # ??Trueprint(hasattr(point1, 'z'))  # ??Trueprint(hasattr(point1, 'no'))  # ??False

4. getattr()

getattr(object, name, default)?????????????????

???

class A(object):    bar = 1a = A()print(getattr(a, 'bar'))  # ??1print(getattr(a, 'bar2', 3))  # ??3print(getattr(a, 'bar2'))  # ??AttributeError

5. setattr()

setattr(object, name, value)?????????????????????

???

class A(object):    bar = 1a = A()print(getattr(a, 'bar'))  # ??1setattr(a, 'bar', 5)print(a.bar)  # ??5setattr(a, "age", 28)print(a.age)  # ??28

6. delattr()

delattr(object, name)??????????

???

class Coordinate:    x = 10    y = -5    z = 0point1 = Coordinate()print('x = ', point1.x)  # ??x = 10print('y = ', point1.y)  # ??y = -5print('z = ', point1.z)  # ??z = 0delattr(Coordinate, 'z')print('--?? z ???--')  # ??--?? z ???--print('x = ', point1.x)  # ??x = 10print('y = ', point1.y)  # ??y = -5

7. property()

property([fget[, fset[, fdel[, doc]]])????????????????fget?fset?fdel??????????????????????

???

class C(object):    def __init__(self):        self.__x = None    def getx(self):        return self.__x    def setx(self, value):        self.__x = value    def delx(self):        del self.__x    x = property(getx, setx, delx, "I'm the 'x' property.")cc = C()cc.x = 2print(cc.x)  # ??2del cc.xprint(cc.x)  # ??AttributeError

????????????????????????????????????????????????????????????????????

转载地址:http://tykp.baihongyu.com/

你可能感兴趣的文章
python商品数据分析可视化系统(带爬虫)京东销售数据分析 计算机毕业设计 源码下载
查看>>
python商品库存管理系统 django框架 商品网站 MySQL数据库 源码下载 计算机毕业设计
查看>>
Python哪个版本最稳定好用2023.10.19
查看>>
Python和黑客技术的渊源
查看>>
Python和RF编写接口自动化
查看>>
Python和RF编写web自动化
查看>>
python和js哪个难_【Python】记一次学习,Python 与 js 在实现闭包时的差异
查看>>
python和java哪个更值得学——来自知乎高赞回答
查看>>
python和c混合编程 github_在pycharm中使用git版本管理以及同步github的方法
查看>>
python命名空间更改_Python模块xml.etree.ElementTree自动修改xml命名空间键
查看>>
Python命名中尾随下划线有什么好处?
查看>>
Python启动脚本
查看>>
Python听写汇总结果
查看>>
python吊打Excel?屁!那是你不会用!
查看>>
Python合并两张图片 | 先叠透明度再合并 (附Demo)
查看>>
python各进制的表述与转换
查看>>
python各种库安装
查看>>
python可视化matplotlib_如何使用Python中最强大的可视化工具Matplotlib?
查看>>
python可维护性_使用这7大神器,让你的Python 代码更容易于维护
查看>>
Python只运行一次while循环
查看>>