博客
关于我
天池龙珠计划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/

你可能感兴趣的文章
opencv videocapture读取视频cap.isOpened 输出总是false
查看>>
opencv waitKey() 函数理解及应用
查看>>
OpenCV 中的图像转换
查看>>
OpenCV 人脸识别 C++实例代码
查看>>
OpenCV 在 Linux 上的 python 与 anaconda 无法正常工作.收到未实现 cv2.imshow() 的错误
查看>>
Opencv 完美配置攻略 2014 (Win8.1 + Opencv 2.4.8 + VS 2013)上
查看>>
opencv 模板匹配, 已解决模板过大程序不工作的bug
查看>>
OpenCV 错误:(-215)size.width>0 &&函数imshow中的size.height>0
查看>>
opencv&Python——多种边缘检测
查看>>
opencv&python——高通滤波器和低通滤波器
查看>>
OpenCV-Python接口、cv和cv2的性能比较
查看>>
opencv1-加载、修改、保存图像
查看>>
opencv10-形态学操作
查看>>
opencv11-提取水平直线和垂直直线
查看>>
opencv12-图像金字塔
查看>>
opencv14-自定义线性滤波
查看>>
opencv15-边缘处理
查看>>
opencv16-Sobel算子
查看>>
opencv17-laplance算子
查看>>
opencv2-矩阵掩膜操作
查看>>