zoukankan      html  css  js  c++  java
  • day28

      " " "

    今日内容:

        1,绑定方法与非绑定方法

       classmethod
            staticmethod

    2.反射

        hasattr
        setattr
        getattr
        delattr

    3.内置的方法

     __str__
            __del__

        " " "

    类中定义函数分为了两大类:

        1.绑定方法

           特殊之处:绑定给谁就应该由谁来调用,谁来调用就会将谁当做第一参数自动转入

               绑定给对象的方法:在类中定义函数没有被任何装饰器 的情况下,默认就是绑定对象的

                绑定给类的方法:为类中定义函数添加一个装饰器classmethod,就是绑定类的

          2,非绑定方法

                特殊之处:非绑定方法就是一个普通函数,既不与类绑定又不与对象绑定,意味着类与对象都可以调用,但是无论谁来调用都是一个普通函数,没有自动转值效果

                  非绑定方法:为类中定义函数添加一个装饰器staticmethod,就是非绑定方法

       ' ' '

    class Foo:
         def func1(self):
             print('func1',self)
    
         @classmethod
         def func2(cls):
             print('func2',cls)
    
         @staticmethod
         def func3(x,y):
             print('func3',x,y)
    
     obj=Foo()

    一。绑定给对象的方法,类也可以调用,但是类调用就是一个普通函数,没有自动传值的效果

    print(obj.func1)
     print(Foo.func1)
     Foo.func1(obj)

    二,绑定给类的方法

         绑定给类的,应该由类来调

    print(Foo.func2)
     print(obj.func2)
     Foo.func2()
     obj.func2()

    三,非绑定方法

    print(obj.func3)
     print(Foo.func3)
    
     obj.func3(1,2)
     Foo.func3(1,3)
    
    
    import settings
    
    class MySQL:
        def __init__(self,ip,port):
            self.id=self.create_id()
            self.ip=ip
            self.port=port
    
        def tell_info(self):
            print('<%s:%s:%s>' % (self.id,self.ip, self.port))
    
        @classmethod
        def from_conf(cls):
            return cls(settings.IP, settings.PORT)
    
        @staticmethod
        def create_id():
            import uuid
            return uuid.uuid4()
    
    obj=MySQL('1.1.1.1',3306)
     obj1=MySQL('1.1.1.2',3406)
    obj.tell_info()
     obj1.tell_info()
    
     obj2=MySQL.from_conf()
     obj2.tell_info()

      ' ' '

    反射值的是通过字符串来操作属性

      ' ' '

    class Foo:
        def __init__(self,name,age):
            self.name=name
            self.age=age
    
        def tell_info(self):
            print('%s:%s' %(self.name,self.age))
    
    obj=Foo('egon',18)
    
    hasattr
     print(hasattr(obj,'name')) #obj.name
     print(hasattr(obj,'tell_info')) #obj.tell_info
    
    getattr
     res=getattr(obj,'name') #res=obj.name
     print(res)
     res=getattr(obj,'xxx',None)
     print(res)
    
    setattr
     setattr(obj,'age',38)
     setattr(obj,'sex','male')
     print(obj.__dict__)
     print(obj.sex)
    
    delattr
     delattr(obj,'name')
    if hasattr(obj,'xxxxe'):
        delattr(obj,'xxxxe')
     print(obj.__dict__)

    内置方法:

     print(isinstance([],list)) #type([]) is list
     class Foo:
         pass
     obj=Foo()
     print(isinstance(obj,Foo))
    
     issubclass()
     class Foo:
         pass
    
     class Bar(Foo):
         pass
     print(issubclass(Bar,Foo))

    _str_;会在对象被打印自动触发,然后将返回值返回给print功能进行打印

    class People:
         def __init__(self,name,age):
             self.name=name
             self.age=age
    
         def __str__(self):
             return '<%s:%s>' %(self.name,self.age)
    
     peo=People('egon',18)
     print(peo) #print(peo.__str__())
    
     l=list([1,2,3])
     print(l)
    

      _del_:会在对象被删除时自动触发执行,用来在对象被删除前回收资源

     class Foo:
         def __del__(self):
             print('===>')
    
     obj=Foo()
      del obj
     print('其他代码...')
    
    class Bar:
        def __init__(self,x,y,filepath):
            self.x=x
            self.y=y
            self.f=open(filepath,'r',encoding='utf-8')
        def __del__(self):
            # 写回收系统资源相关的代码
            self.f.close()
    
    obj=Bar(10,20)
    del obj

    settings

    lP='10,10,0,11'

    PORT=3307

  • 相关阅读:
    算法面试题解答(三)
    计算机基础知识问答
    算法面试题解答(五)
    关于POD
    算法面试题解答(四)
    算法面试题解答(一)
    Perfect Interview (序)
    如何调整Dreamhost主机PHP上传尺寸的限制/How to change the maximal size of uploading in your Dreamhost
    Final Fantasy XIII Finished
    Ortholab has been moved to Google Code
  • 原文地址:https://www.cnblogs.com/hui2002/p/9850974.html
Copyright © 2011-2022 走看看