Binding and unbound methods of a class

The functions defined in the class are divided into two categories

One: Binding method (who is bound to, whoever calls it will automatically pass in itself as the first parameter):

  1. Methods bound to classes: methods decorated with the classmethod decorator.

````
Tailored for the class

    类.boud_method(),自动将类当作第一个参数传入

  (其实对象也可调用,但仍将类当作第一个参数传入)

```

  1. Methods bound to objects: methods that are not decorated by any decorators.

````
Tailored to the object

   对象.boud_method(),自动将对象当作第一个参数传入

 (属于类的函数,类可以调用,但是必须按照函数的规则来,没有自动传值那么一说)

```

Two: Unbound methods: methods decorated with staticmethod decorators

Not bound to a class or object, both classes and objects can be called, but there is no such thing as automatic value transfer. It's just an ordinary tool

Note: Different from methods bound to objects, functions defined directly in a class that are not decorated with any decorator are methods bound to objects, not ordinary functions. Objects will automatically pass values ​​when calling this method. And the method decorated by staticmethod, no matter who calls it, there is no automatic value transfer.

binding method

Methods bound to objects (omitted)

A method bound to a class (classmethod)

classmehtod is for classes, that is, bound to the class. When the class is used, the class itself will be passed as a parameter to the first parameter of the class method (even if it is called by an object, the class will be passed in as the first parameter) ), python has built-in function classmethod for us to define the functions in the class as class methods

#settings.py
HOST='127.0.0.1'
PORT=3306
DB_PATH=r'C:\Users\Administrator\PycharmProjects\test\面向对象编程\test1\db'

#test.py
import settings
class MySQL:
    def __init__(self,host,port):
        self.host=host
        self.port=port

    @classmethod
    def from_conf(cls):
        print(cls)
        return cls(settings.HOST,settings.PORT)

print(MySQL.from_conf) #<bound method MySQL.from_conf of <class '__main__.MySQL'>>
conn=MySQL.from_conf()

conn.from_conf() #对象也可以调用,但是默认传的第一个参数仍然是类

unbound method

A function decorated with staticmethod inside a class is an unbound method, which is a normal function

Statimethod is not bound to classes or objects, anyone can call it, and there is no automatic value transfer effect

import hashlib
import time
class MySQL:
    def __init__(self,host,port):
        self.id=self.create_id()
        self.host=host
        self.port=port
    @staticmethod
    def create_id(): #就是一个普通工具
        m=hashlib.md5(str(time.time()).encode('utf-8'))
        return m.hexdigest()


print(MySQL.create_id) #<function MySQL.create_id at 0x0000000001E6B9D8> #查看结果为普通函数
conn=MySQL('127.0.0.1',3306)
print(conn.create_id) #<function MySQL.create_id at 0x00000000026FB9D8> #查看结果为普通函数

classmethod vs staticmethod

import settings
class MySQL:
    def __init__(self,host,port):
        self.host=host
        self.port=port

    @staticmethod
    def from_conf():
        return MySQL(settings.HOST,settings.PORT)

    # @classmethod #哪个类来调用,就将哪个类当做第一个参数传入
    # def from_conf(cls):
    #     return cls(settings.HOST,settings.PORT)

    def __str__(self):
        return '就不告诉你'

class Mariadb(MySQL):
    def __str__(self):
        return '<%s:%s>' %(self.host,self.port)


m=Mariadb.from_conf()
print(m) #我们的意图是想触发Mariadb.__str__,但是结果触发了MySQL.__str__的执行,打印就不告诉你:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325282140&siteId=291194637