69.Python:非绑定方法staticmethod


import settings


# 二、非绑定方法 ==> 静态方法
# 没有绑定给任何人:调用者可以是类、对象,没有自动传参的效果
class Mysql:
def __init__(self, ip, port):
self.nid = self.creat_id()
self.ip = ip
self.port = port

@staticmethod # 将下述函数装饰成一个静态方法
def creat_id():
import uuid
return uuid.uuid4()


obj1 = Mysql('1.1.1.1', 8805)
print(Mysql.creat_id)
print(obj1.creat_id)

Mysql.creat_id()
obj1.creat_id()
posted @ 2021-07-06 17:56  SEPIA  阅读(39)  评论(0)    收藏  举报