博客
关于我
天池龙珠计划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 开发者,这 7 个 VS Code 插件极力推荐
查看>>
python手把手视频_硬货 | 手把手带你构建视频分类模型(附Python演练))
查看>>
python 录音左右声道_Python分离立体声wav压缩文件的左右声道
查看>>
Python 循环异或对文件进行加解密
查看>>
Python开发环境搭建(附VMware安装包及虚拟机环境)
查看>>
python 微信扫码登录_python实现微信第三方网站扫码登录(Django)
查看>>
Python 快速下载依赖
查看>>
python实现非参数统计的Cochran检验 (附完整源码)
查看>>
Python 手写数字识别-3-sklearn中的几种算法
查看>>
Python 打开文件注意事项
查看>>
python 批量修改文件名_python windows下批量修改文件名
查看>>
Python 抓取网页乱码问题 以及EXCEL乱码
查看>>
python 抓取网页内容
查看>>
python 按照当前日期创建文件
查看>>
Python 接口并发测试详解
查看>>
Python 接口自动化 —— requests框架
查看>>
python 接口自动化数据结构(如列表、字典、元组)
查看>>
Python 接口自动化测试中的深拷贝与浅拷贝~
查看>>
Python 接口自动化测试中的高阶函数
查看>>
python 控制 cmd 命令行颜色
查看>>