Skip to main content
 首页 » 编程设计

python之无法在另一个python脚本中使用python函数的属性

2025年05月04日10youxin

我想将 python 脚本 (x) 中的函数返回的属性之一用于 python 脚本 (y)

两个脚本之间的通信运行良好;我可以获取函数和属性,但不允许我获取函数返回的属性。

这是我的工作方式:

x.py

def func(): 
    b = 10 
    a = 15 
    return [a,b] 
c = 20 

y.py

from x import func 
import x 
print (x.c) 
print (func.b) 

我得到“c”值和以下错误 AttributeError: 'function' object has no attribute 'b'

我也尝试过打印 x.b,但我遇到了 AttributeError: module 'WorkingLSTM' has no attribute 'b'

提前致谢

请您参考如下方法:

调用 func 的方法是使用 func(),它会给你 [a,b]。

例子:

funcResult = func() 
 
a = funcResult[0] 
b = funcResult[1] 

funcResults 是func() 的返回值,即列表[a,b]。