gpt4 book ai didi

python - 从另一个文件中的函数导入文件

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:03 24 4
gpt4 key购买 nike

我需要调用我在另一个文件中的函数中创建的列表。我尝试在下面尝试此操作,但收到错误cannot import name 'names' from 'backend'。有谁知道如何在不类的情况下实现这一目标?

import backend
from backend import names
word = names
print (word)

错误消息:

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'

另一个文件的代码是

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'


with open(const.DATA_FILENAME) as f:
header_to_col_num = parse_header(f)
for line in f:
info = line.split(const.SEP)
name = (info[(header_to_col_num['pokemon'])])
col_names = ('pokemon', 'species_id', 'height', 'weight', 'type_1',
'type_2',
'url_image', 'generation_id', 'evolves_from_species_id',)
value = [info[header_to_col_num[col]] for col in col_names]
value[1] = int(value[1])
value[2] = float(value[2])
value[3] = float(value[3])
value[7] = int(value[7])
value = tuple(value)

name_to_stats[name] = value

species_id = int(info[(header_to_col_num['species_id'])])
id_to_name[species_id] = name
names.append(name)


for name, info in name_to_stats.items():
type1 = info[4]
type2 = info[5]
if type1 in pokemon_by_type:
pokemon_by_type[type1].append(name)
else:
pokemon_by_type[type1] = [name]
if type2 in pokemon_by_type:
pokemon_by_type[type2].append(name)
else:
pokemon_by_type[type2] = [name]


return name_to_stats, id_to_name, names, pokemon_by_type

最佳答案

根据Python's documentation :

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

因此,您无法从另一个文件导入 names 列表的原因是 names 位于您的 get_pokemon_stats 函数作用域内,并且它不是一个全局变量。

您可以将 names 设为全局,将其放在函数之外,并将其声明为全局以在函数内部使用:

...
names = []
def get_pokemon_stats():
...
global names
...

但是,如果你真的想这样做,你应该仔细考虑。仅当您调用 get_pokemon_stats 函数时,names 才会包含实际值。尽管如此,如果您不真正了解局部变量和全局变量如何工作以及何时应该使用它们,则应该避免仅在全局范围内声明变量。

我建议您考虑执行以下代码:

from backend import get_pokemon_stats
_, _, word, _ = get_pokemon_stats()
print (word)

关于python - 从另一个文件中的函数导入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53420278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com