Python common knowledge, static methods and class methods

1, static methods

Import Requests 

class Http: 

    # static methods of a class, call the need to create objects without the use of Self 
    @staticmethod
     DEF GET (url, return_json = True): 

        r = requests.get (url) 

        IF r.status_code == 200 :
             return r.json () IF return_json the else r.text
         the else :
             return {} IF return_json the else  ''

2, class methods

class Book:
     # property defaults to class attributes (class itself may be invoked directly to) 
    isbn_url = ' http://127.0.0.1:5000/book/search/ {} ' 

    # class method (class instance may not need to be class call itself) 
    @classmethod
     DEF search_by_isbn (CLS, ISBN): 

        # CLS: represents useless itself is instantiated 
        URL = cls.isbn_url.format (ISBN) 

        Result = Http.get (URL)
         return Result

 

Guess you like

Origin www.cnblogs.com/tangqiu/p/12565782.html