1、静态方法
import requests class Http: #类的静态方法,调用不需要创建对象,不需要使用self @staticmethod def get(url,return_json=True): r=requests.get(url) if r.status_code==200: return r.json() if return_json else r.text else: return {} if return_json else ''
2、类方法
class Book: # 属性默认为类属性(可以给直接被类本身调用) isbn_url='http://127.0.0.1:5000/book/search/{}' #类方法(不需要实例化类就可以被类本身调用) @classmethod def search_by_isbn(cls,isbn): #cls : 表示没用被实例化的类本身 url=cls.isbn_url.format(isbn) result=Http.get(url) return result