先來看看__call__什么時候時候可以調(diào)用
class Aninmal(object):
def __init__(self,name):
self.name=name
def __call__(self, *args, **kwargs):
print('姓名是:%s'%self.name)
a=Aninmal('小明')
不返回任何結(jié)果
class Aninmal(object):
def __init__(self,name):
self.name=name
def __call__(self, *args, **kwargs):
print('姓名是:%s'%self.name)
a=Aninmal('小明')
a()
有結(jié)果返回
D:\python38\python.exe D:/pyprogram/vuefronted/dbinit.py
姓名是:小明
注意這個__call__必須要實例化之后才可以調(diào)用不然不會調(diào)用
class Aninmal(object):
def __call__(self, *args, **kwargs):
print('姓名')
a=Aninmal()
無任何結(jié)果返回
內(nèi)置函數(shù)同樣可以用__call__()方法來調(diào)用
def fun():
print("hello world")
fun()
fun.__call__()
自定義函數(shù)也也可以通過__call__()方法來調(diào)用
print(int(3))
print(int.__call__(3))
接下來理解為什么
from flask import Flask
app=Flask(__name__)
if __name__ == '__main__':
app.run(debug=False)
這個最后會調(diào)取Flask的__call__方法
from werkzeug.serving import run_simple
def func(environment,start_response):
print("請求來了")
pass
if __name__=='__main__':
run_simple('127.0.0.1',5000,func)
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
瀏覽器輸入http://127.0.0.1:5000 這個之后文章來源:http://www.zghlxwxcb.cn/news/detail-605178.html
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
請求來了
127.0.0.1 - - [21/Jul/2023 13:20:04] "GET / HTTP/1.1" 500 -
看到?jīng)]有執(zhí)行了func程序,所以會調(diào)用這個文章來源地址http://www.zghlxwxcb.cn/news/detail-605178.html
- 技術(shù)無止境
到了這里,關(guān)于flask啟動為什么會調(diào)用__call__的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!