第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從另一個(gè)文件中的函數(shù)導(dǎo)入文件

從另一個(gè)文件中的函數(shù)導(dǎo)入文件

慕妹3242003 2021-08-24 18:07:34
我需要調(diào)用我在另一個(gè)文件中的函數(shù)中創(chuàng)建的列表。我試圖在下面嘗試這個(gè),但我收到了錯(cuò)誤cannot import name 'names' from 'backend'。有誰(shuí)知道如何在不上課的情況下實(shí)現(xiàn)這一目標(biāo)?import backendfrom backend import namesword = namesprint (word)錯(cuò)誤信息:File "C:/Users/user/OneDrive/Desktop/Pokemon/weather.py", line 52, in <module>  from backend import names  builtins.ImportError: cannot import name 'names' from 'backend'另一個(gè)文件的代碼是import const SEP = ','def get_pokemon_stats():    """Reads the data contained in a pokemon data file and parses it into    several data structures.    Args:        None    Returns: a tuple of:        -a dict where:            -each key is a pokemon name (str)            -each value is a tuple of the following stats:                -pokemon name (str)                -species_id (int)                -height (float)                -weight (float)                -type_1 (str)                -type_2 (str)                -url_image (str)                -generation_id (int)                -evolves_from_species_id (str)        -a dict where:            -each key is a pokemon species_id (int)            -each value is the corresponding pokemon name (str)        -a list of all pokemon names (strs)        -a dict where:            -each key is a pokemon type (str). Note that type_1 and type_2            entries are all considered types. There should be no special            treatment for the type NA; it is considered a type as well.            -each value is a list of all pokemon names (strs) that fall into            the corresponding type    """    name_to_stats = {}    id_to_name = {}    names = []    pokemon_by_type = {}    DATA_FILENAME = 'pokemon.csv' 
查看完整描述

2 回答

?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊

根據(jù)Python 的文檔:


在 Python 中,僅在函數(shù)內(nèi)部引用的變量是隱式全局變量。如果在函數(shù)體內(nèi)的任何地方為變量賦值,則假定它是局部變量,除非明確聲明為全局變量。


因此,您不能names從另一個(gè)文件導(dǎo)入您的列表的原因是因?yàn)閚ames它在您的get_pokemon_stats函數(shù)范圍內(nèi)并且它不是一個(gè)全局變量。


您可以names將其設(shè)為 global 將其放在您的函數(shù)之外,并將其聲明為 global 以在您的函數(shù)內(nèi)使用:


...

names = []

def get_pokemon_stats():

    ...

    global names

    ...

但是,如果您真的想這樣做,您應(yīng)該仔細(xì)考慮。names一旦您調(diào)用您的get_pokemon_stats函數(shù),將只包含實(shí)際值。盡管如此,如果您不真正了解局部和全局變量的工作原理以及何時(shí)應(yīng)該使用它們,則應(yīng)避免僅全局聲明變量。


我建議您考慮改為執(zhí)行以下代碼:


from backend import get_pokemon_stats

_, _, word, _ = get_pokemon_stats()

print (word)


查看完整回答
反對(duì) 回復(fù) 2021-08-24
?
繁星點(diǎn)點(diǎn)滴滴

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊

您需要調(diào)用該get_pokemon_stats函數(shù)。它返回四個(gè)值,第三個(gè)值是names。


import backend

name_to_stats, id_to_name, names, pokemon_by_type = backend.get_pokemon_stats()

print(names)


查看完整回答
反對(duì) 回復(fù) 2021-08-24
  • 2 回答
  • 0 關(guān)注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)