不需要重新运行
SQL_Country()
第二次作为
df
df_country = None
def SQL_Country():
if df_country is None:
conn = psq.connect("localhost","root","root","world",cursorclass=psq.cursors.DictCursor)
query = "SELECT * FROM country"
with conn:
cursor = conn.cursor()
cursor.execute(query)
country = cursor.fetchall()
df_country = pd.DataFrame(country, columns=["Code", "Name", "Continent", "Population", "HeadOfState"])
return df_country
现在当你调用这个函数时,它不会第二次执行它,但是你会得到你想要的值
def assignment():
## do something here
if Choice == 6:
# No need to do the name check
x = SQL_Country()
if x is None:
print()