• python3 面向对象补充



    f = People('egon',18,'male')

    非函数
    hasattr # hasattr(f,'name')
    getattr # getattr(f,'name')
    setattr # setattr(f,'name','EGON')
    class Teacher:
        school = 'oldboy'
        def __init__(self,name,age):
            self.name = name
            self.age = age
    
        def teach(self):
            print(self.name)
    
    # print(hasattr(Teacher,'school'))
    # print(getattr(Teacher,'school'))
    # print(getattr(Teacher,'asdfadsf','没有这个'))
    # setattr(Teacher,'school','欧德博爱')
    # print(Teacher.school)
    # delattr(Teacher,'school')
    # print(Teacher.__dict__)
    
    
    t = Teacher('egon',18)
    
    # print(hasattr(t,'name'))
    # print(getattr(t,'name'))
    # setattr(t,'name','EGON')
    # print(getattr(t,'name'))
    #
    # print(t.__dict__)
    # delattr(t,'name')
    # print(t.__dict__)
    
    # print(getattr(t,'teach'))
    # print(getattr(t,'school'))
    # print(t.school)
    # t.school = 'hahhahahahha'
    # print(getattr(Teacher,'school'))
    # print(getattr(t,'school'))
    # print(t.__dict__)

    item系列
    __getitem__ # f['name']
    __setitem__ # f['name'] = 'EGON'
    __delitem__ # del f['name']
    __len__ # len(f)
    class Foo:
        def __init__(self,name,age,sex):
            self.name=name
            self.age=age
            self.sex=sex
        def __getitem__(self, item):
            # print(self,item,type(item))
            # return getattr(self,item)
            return self.__dict__[item]
        def __setitem__(self, key, value):
            # setattr(self,key,value)
            self.__dict__[key]=value
    
        def __delitem__(self, key):
            # delattr(self,key)
            self.__dict__.pop(key)
    
        def __len__(self):
            return 10
    f=Foo('egon',18,'male')
    # print(f.name) #f['name']
    # print(f.age) #f['age']
    # print(f.sex) #f['sex']
    
    # print(f['name'])
    
    # f['name']='egon_nb'
    # print(f.__dict__)
    # del f['name']
    # print(f.__dict__)
    
    print(len(f))

    f = People
    __str__ # print(f) # f.__str__()
    class Teacher:
        def __init__(self,name,age):
            self.name=name
            self.age=age
            self.courses=[]
    
        def teach(self):
            print('%s teach' %self.name)
    
        def __str__(self):
            return '<name:%s age:%s>' %(self.name,self.age)
    
    class Course:
        def __init__(self,name,price,period):
            self.name=name
            self.price=price
            self.period=period
        def __str__(self):
            return '《name:%s price:%s period:%s》' %(self.name,self.price,self.period)
    
    # egon=Teacher('egon',18)
    # print(egon) #egon.__str__()
    # print(egon) #egon.__str__()

    __del__ # del f # f.__del__()
    import time
    class Foo:
        def __init__(self,x):
            self.x=x
            print('connect mysql') #conn=abcdef('192.168.1.10',3306)
    
        def __del__(self):
            '''做一些与这个对象有关的清理操作'''
            # conn.close()
            # file.close()
            print('====>')
    f=Foo(10)
    del f #f.__del__()
    time.sleep(3)
    print('主程序')
  • 相关阅读:
    为什么使用C#开发软件的公司和程序员都很少?
    使用Redis之前5个必须了解的事情
    这段代码为什么捕获不到异常呢?谁能给个解释,谢谢。
    git报错
    C# 常用类库(字符串处理,汉字首字母拼音,注入攻击,缓存操作,Cookies操作,AES加密等)
    你所不知道的 CSS 负值技巧与细节
    CSS 属性选择器的深入挖掘
    探秘 flex 上下文中神奇的自动 margin
    CSS 火焰?不在话下
    不可思议的纯 CSS 实现鼠标跟随效果
  • 原文地址:https://www.cnblogs.com/lucaq/p/7133334.html
Copyright © 2020-2023  润新知