zoukankan      html  css  js  c++  java
  • python 绑定方法

    一.绑定方法与非绑定方法

    1.绑定方法
    '''
       绑定方法:绑定给谁就是给谁用的

        1.凡是类中的方法和函数,都是绑定给对象使用的;

        2.绑定方法都有自动传值的功能。传递进去的值,就是对象本身。

        3.如果类想调用绑定方法,就必须遵循函数的参数规则,有几个参数,就必须传递几个参数。

    绑定到对象的方法:
    定义:凡是在类中定义的函数(没有被任何装饰器修饰),都是绑定给对象的,
    给谁用:给对象用
    特点:obj.bar() 自动把obj当做第一个参数传入,因为bar中的逻辑就是要处理obj
    绑定到类的方法:
    定义:在类中定义的,被@classmethod装饰的函数就是绑定到类的方法
    给谁用:给类用
    特点:类.class_method() 自动把类当做第一个参数传入,因为class_method中的逻辑就是要处理类
    '''
    class People:
        def __init__(self,name):
            self.name=name
        def bar(self):
            print('--->',self.name)
    
        @classmethod
        def func(cls):          --------->绑定类的方法,默认为cls
            print(cls)
    f=People('egon')
    # print(People.func)       ####绑定给类
    # print(f.bar)      ####绑定给对象的
    
    # People.func()
    
    # f.func()
    View Code
    #####绑定类的方法
    
    class MySQL:
        def __init__(self,ip,port):
            self.ip=ip
            self.port=port
    
        @classmethod       ---->绑定到类的方法方便我们直接产生MYSQL对象,以
        def from_conf(cls):   #便于我们设置相应的条件来控制
            import settings    ##settings.py文件里存放IP与地址
            obj=cls(settings.ip,settings.port) #obj=MySQL('1.1.1.1',8080)
            obj.x=111111111111111111111111111111111
            return obj
    
    # m=MySQL('127.0.0.1',8080)
    
    obj=MySQL.from_conf()
    
    print(obj.ip)
    print(obj.port)
    print(obj.x)
    View Code

      2.非绑定方法

      非绑定方法就是一个函数,就是一个工具而已,不需要类,也不需要对象,没有自动传值这么一说.

      在面向对象中实现,就是 @staticmethod

    import hashlib
    import time
    import pickle
    import os
    student_path=r'C:UsersAdministratorPycharmProjectspython5期day22student'
    
    class People:
        def __init__(self,name,sex,user_id):
            self.name=name
            self.sex=sex
            self.user_id=user_id
            self.id = self.create_id()
    
        def tell_info(self):
            print('''
            --------%s info--------
            id:%s
            name:%s
            sex:%s
            user_id:%s
            ''' %(self.name,self.id,self.name,self.sex,self.user_id))
    
        def create_id(self):
            m=hashlib.md5()
            m.update(self.name.encode('utf-8'))
            m.update(self.sex.encode('utf-8'))
            m.update(str(self.user_id).encode('utf-8'))
            return m.hexdigest()
    
        def save(self):
            with open(self.id,'wb') as f:
                pickle.dump(self,f)
    
        @staticmethod   #非绑定方法,就是一个函数,就是一个工具而已,不需要类,也不需对象
        def get_all():
            res = os.listdir(student_path)
            for item in res:
                file_path=r'%s\%s' %(student_path,item)
                with open(file_path,'rb') as f:
                    obj=pickle.load(f)
                    obj.tell_info()
    
    
    # print(People.get_all)
    #
    # p=People('egon','male',123123123)
    # print(p.get_all)
    View Code

      

  • 相关阅读:
    leetcode1078
    leetcode417
    我在机器学习踩过的坑,现在告诉你怎么跳过去
    Python相关机器学习‘武器库’
    分别列举人工智能4个主要领域中最牛叉的10位专家
    多巴胺:谷歌开源新型增强学习框架
    AI产品经理成长路
    一个AI产品经理怎么看AI的发展
    机器学习的数学基础
    方差variance, 协方差covariance, 协方差矩阵covariance matrix
  • 原文地址:https://www.cnblogs.com/junxiansheng/p/7126747.html
Copyright © 2011-2022 走看看