python - flask中,instance_path參數(shù)的存在意義是什么呢?
問(wèn)題描述
flask的Flask類初始化參數(shù)中有一個(gè)instance_path,默認(rèn)為None。
在官網(wǎng)上看了相關(guān)文檔Instance Folders,反復(fù)看了幾遍還是不太明白其意義何在,看flask相關(guān)案例也沒(méi)有見(jiàn)過(guò)使用的。自己在機(jī)器上做了下測(cè)試,輸出如下:view函數(shù)加一句print current_app.root_path+’n’+current_app.instance_path,可以得到:
/home/xxx/Documents/flasky/flasky/app/home/xxx/Documents/flasky/flasky/instance
其跟root_path的區(qū)別僅在于最后一個(gè)地方由app變成了instance,這參數(shù)是0.8版本才加的,意義何在(既然它和root_path基本一樣),加了它之后能夠?qū)崿F(xiàn)什么之前所不能實(shí)現(xiàn)的東西嗎?
問(wèn)題解答
回答1:看了下文檔里面有這樣一段話,不知道能不能解答題主的疑惑:
The instance folder is designed to not be under version control and be deployment specific. It’s the perfect place to drop things that either change at runtime or configuration files.
我的理解就是類似前端html與css分離一樣,實(shí)例文件夾與默認(rèn)的跟文件夾分離,降低耦合度。方便后期修改實(shí)例的時(shí)候不影響到跟文件夾。題主說(shuō)的None就是默認(rèn)的實(shí)例是在根文件夾下面的,對(duì)應(yīng)到題目中的例子就是在flasky目錄下面。而現(xiàn)在新的版本可以通過(guò)設(shè)置instance_path參數(shù)指定具體的位置,比方說(shuō)跟flasky目錄同級(jí)。
相關(guān)文章:
