我正在查看我的一些代碼的執行流程,我想知道下面是否會起作用。如果'if'條件已經運行,Python會跳過讀'else'條件嗎?
具體來說,我正在查看此條件樹中的else
子句。如果在內存中沒有指定配置路徑,我會得到一個將配置路徑作爲輸入的函數。假設我給出正確的輸入。在declareConfPath()
之後,計算機沒有理由運行嵌入條件,該條件檢查在運行declareConfPath()
時是否指定了任何內容。
我的問題是,如果該程序會跳過該else
情況下,或者如果它讀取else
情況下,將採取由declareConfPath()
第一if
情況下,在樹中指定的confPath
新的價值。如果它不跳過,那麼我已經解決了所有需要的條件,而不是另一種涉及另一棵樹的替代解決方案。如果沒有,那麼我需要複製幾行代碼。這並不昂貴,但也不是很優雅。
也可能出現的情況是,使用elif
而不是if
可能會得到我想要的結果,但我不知道。
confPath = None; # or if the file when opened is empty?
_ec2UserData = None;
localFile = False;
# As we are dealing with user input and I am still experimenting with what information counts for a case, going to use try/except.
# Checks if any configuration file is specified
if confPath == None: #or open(newConfPath) == False:
# Ask if the user wants to specify a path
# newConfPath.close(); <- better way to do this?
confPath = declareConfPath();
# If no path was specified after asking, default to getting values from the server.
if confPath == None:
# Get userData from server and end conditional to parsing.
_ec2UserData = userData(self);
# If a new path was specified, attempt to read configuration file
# Does the flow of execution work such that when the var is changed, it will check the else case?
else confPath != None:
localFile = True;
fileUserData = open(confPath);
'else'沒有條件。使用'else:'或'elif condition:'。 – Hyperboreus