class A(type):
sql = ""
def __enter__(self):
self.db_conn = self.sql + "123"
print("enter")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print(self.db_conn)
print("exit")
self.db_conn = ""
class B(metaclass=A):
sql = "sql"
@classmethod
def tt(cls):
print(cls.sql, "---")
print(cls.db_conn, "***")
with B as b:
b.tt()
# enter
# sql ---
# sql123 ***
# sql123
# exit
python with使用类名 classmethod
最新推荐文章于 2023-12-25 23:11:15 发布