博客
关于我
天池龙珠计划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 str与bytes之间的转换
查看>>
Python subprocess ffmpeg
查看>>
python subprocess Permission denied Errno 13
查看>>
Python subprocess.call - 将变量添加到 subprocess.call
查看>>
Python Subprocess.Popen 从一个线程
查看>>
Python subprocess.Popen 作为 Windows 上的不同用户
查看>>
Python转换PPT为PDF
查看>>
Python subprocess.Popen() 等待完成
查看>>
Python sum 二维列表中具有相同第一个值的元素
查看>>
Python Sympy模块NoConversion:收敛到根失败;请尝试n<;15或MaxSteps>;50
查看>>
Python sys.MODULES包含尚未导入的模块
查看>>
python sys模块使用
查看>>
Python tarfile、zipfile解压模块讲解
查看>>
python tcp server传输成功之后进行删除_python学习之路(三)使用socketserver进行ftp断点续传...
查看>>
Python threading.Thread 只能使用私有方法 self.__Thread_stop() 停止
查看>>
python time模块
查看>>
Python Tkinter Multiple Windows 教程
查看>>
Python tkinter 中的多处理
查看>>
python tkinter 按钮颜色_Python3 tkinter基础 Button bg 按钮的背景颜色
查看>>
Python Tkinter 笔记本小部件
查看>>