博客
关于我
天池龙珠计划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 依赖管理的革新——Poetry 深度解析
查看>>
python 保留精度及增加去除数字的千位分隔符(金额化数字)
查看>>
python 倒计时 9,8,7,。。。。。。0
查看>>
Python 入门开发学习笔记之数据的增删改查
查看>>
Python 入门教程(2)搭建环境 2.4、VSCode配置Node.js运行环境
查看>>
Python 内存管理
查看>>
Python 内嵌函数:它们有什么用处?
查看>>
python 利用pyttsx3文字转语音
查看>>
Python 基础语法:基本数据类型(一)
查看>>
Python 基础语法:基本数据类型(字典)
查看>>
Python 基础语法:基本数据类型(集合)
查看>>
python 堆排序
查看>>
python 复习计划
查看>>
Python 多处理 apply_async 永远不会在 Windows 7 上返回结果
查看>>
Python 多处理 Numpy 随机
查看>>
Python 多处理 >= 125 列表永远不会完成
查看>>
Python 多处理:在第一个子错误时中止映射
查看>>
Python 多处理不断产生 pythonw.exe 进程而不做任何实际工作
查看>>
Python 多处理从不加入
查看>>