zoukankan      html  css  js  c++  java
  • 类的绑定方法学习

    类的绑定方法

    对象绑定方法/类的绑定方法

    类的绑定方法:绑定给类的,类来调用,会把类自身传过来

    类的绑定方法的用法

    不需要通过对象,只需要通过类就能获取到一些东西的时候,用类的绑定方法

    类的绑定方法也可以用对象来掉用

    类中使用 @classmethod 修饰的方法就是绑定到类的方法。这类方法专门为类定制。通过类名调用绑定到类的方法时,会将类本身当做参数传给类方法的第一个参数。

    非绑定方法

    在类内部使用 @staticmethod 修饰的方法即为非绑定方法,这类方法和普通定义的函数没有区别,不与类或对象绑定,谁都可以调用,且没有自动传值的效果。

    import hashlib
    
    
    class Operate_database():
        def __init__(self, host, port, user, password):
            self.host = host
            self.port = port
            self.user = user
            self.password = password
    
        @staticmethod
        def get_passwrod(salt, password):
            m = hashlib.md5(salt.encode('utf-8'))  # 加盐处理
            m.update(password.encode('utf-8'))
            return m.hexdigest()
    
    
    hash_password = Operate_database.get_passwrod('lala', '123456')  # 通过类来调用
    print(hash_password)
    
  • 相关阅读:
    查看linux命令类型
    理解bashrc和profile[转载]
    问题:ldconfig
    箭头函数
    闭包函数
    方法
    手把手教你使用百度地图(图解)
    变量作用域与解构赋值
    iterable
    Map和Set
  • 原文地址:https://www.cnblogs.com/cheng825/p/11429943.html
Copyright © 2011-2022 走看看