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

 

相关文章:

  • 2021-09-23
  • 2022-12-23
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2022-02-01
猜你喜欢
  • 2021-09-17
  • 2022-02-24
  • 2021-07-22
  • 2021-06-30
  • 2021-09-17
相关资源
相似解决方案