• 1-15类


     

    类:面向对象

    In [1]:
    class people :
        '帮助信息:'
        #所有实例都会共享
        number=100
        #构造函数,初始化的方法,当创建一个类的时候,首先会调用他 注意:下划线是2个
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def display(self):
            print('number=:',people.number)
        def display_name(self):
            print(self.name)
    
    In [2]:
    people.__doc__
    
    Out[2]:
    '帮助信息:'
    In [3]:
    p1=people('tangyudi',30)
    
    In [4]:
    p2=people('python',40)
    
    In [5]:
    p1.name
    
    Out[5]:
    'tangyudi'
    In [6]:
    p2.name
    
    Out[6]:
    'python'
    In [7]:
    p1.display()
    
     
    number=: 100
    
    In [8]:
    p2.display()
    
     
    number=: 100
    
    In [9]:
    p2.name='hello'#更改
    p2.name
    
    Out[9]:
    'hello'
    In [10]:
    del p2.name#删除
    p2.name#删除后就无法再显示,所以报错
     
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-10-e8841ab000e8> in <module>()
          1 del p2.name#删除
    ----> 2p2.name
    
    AttributeError: 'people' object has no attribute 'name'
    In [11]:
    hasattr(p1,'name')#hasattr是对属性检测是否存在,返回BOOL
    
    Out[11]:
    True
    In [12]:
    hasattr(p1,'sex')
    
    Out[12]:
    False
    In [13]:
    getattr(p1,'name')#获取属性所对应的值
    
    Out[13]:
    'tangyudi'
    In [16]:
    setattr(p1,'name','marujaio')#更改属性所对应的值
    
    In [17]:
    delattr(p1,'name')#删除属性
    
    In [18]:
    getattr(p1,'name')
    
     
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-18-9225ee5d4e45> in <module>()
    ----> 1getattr(p1,'name')#获取属性所对应的值
    
    AttributeError: 'people' object has no attribute 'name'
  • 相关阅读:
    “零接触”新需求,如何快速实现体温检测数字化管控方案?
    AR公共安全及应急指挥中的应用 | TVP思享
    当模板方法遇到了委托函数,你的代码又可以精简了
    为什么要用内插字符串代替string.format
    如何让多个不同类型的后端网站用一个nginx进行反向代理实际场景分析
    8天入门docker系列 —— 第八天 让程序跑在swarm集群上
    8天入门docker系列 —— 第七天 让你的container实现跨主机访问
    8天入门docker系列 —— 第六天 搭建自己的私有镜像仓库Registry
    8天入门docker系列 —— 第五天 使用aspnetcore小案例熟悉容器互联和docker-compose一键部署
    8天入门docker系列 —— 第四天 使用aspnetcore小案例熟悉端口映射和挂载目录
  • 原文地址:https://www.cnblogs.com/AI-robort/p/11627121.html
Copyright © 2020-2023  润新知