在另一个 .py 中导入模块时, return 的功能与直接 运行 不同吗?
When importing a module in another .py , return of function is different to directly run it?
我有以下程序:
def savings_account(client_to_accounts: Dict[Tuple[str, int],
List[List[float]]], valid_client: Tuple[str, int],
balance: float, interest: float)-> list:
''' Return the newest update of accounts added.'''
key_list = client_to_accounts.keys()
for i in key_list:
if i == valid_client:
last = len(client_to_accounts[i][0]) - 1
client_to_accounts[i][0].insert(last,balance)
client_to_accounts[i][1].insert(last,interest)
return client_to_accounts[i]
当我从原始文件中调用这个函数时:
actual = savings_account({('Habib', 696969696): [[1.0, 10000.0], [2.0, 1.0]],
('Hendiye', 123456789): [[20000.0, -100.0], [1.0, 1.0]]},
('Hendiye', 123456789),40.0, 2.0)
print(actual)
#printed: [[20000.0, 40.0, -100.0], [1.0, 2.0, 1.0]]
('Hendiye', 123456789)
的值将正确更新。但是当从其他 python 文件调用此函数时 ('Hendiye', 123456789)
的值不会更新。
import banking_functions
param1 = {('Habib', 696969696): [[1.0, 10000.0], [2.0, 1.0]], ('Hendiye', 123456789): [[20000.0,
-100.0], [1.0, 1.0]]}
param2 = (('Hendiye', 123456789),40.0, 2.0)
param3 = 40.0
param4 = 2.0
actual = banking_functions.savings_account(param1, param2, param3, param4)
#expected = [[20000.0, 40.0, -100.0], [1.0, 2.0, 1.0]]
print(actual)
#printed : [[20000.0, -100.0], [1.0, 1.0]]
您的第二个代码片段调用函数的方式与第一个代码片段不同。
您的意思是:
import banking_functions
param1 = {('Habib', 696969696): [[1.0, 10000.0], [2.0, 1.0]], ('Hendiye', 123456789): [[20000.0,
-100.0], [1.0, 1.0]]}
param2 = ('Hendiye', 123456789) # Just a tuple of two items
param3 = 40.0
param4 = 2.0
actual = banking_functions.savings_account(param1, param2, param3, param4)
#expected = [[20000.0, 40.0, -100.0], [1.0, 2.0, 1.0]]
print(actual)
现在输出与第一个相同。
肯定存在值错误,您的代码在这两种情况下给出了相同的答案。
来自文件:
from typing import List,Dict,Tuple
def savings_account(client_to_accounts: Dict[Tuple[str, int],
List[List[float]]], valid_client: Tuple[str, int],
balance: float, interest: float)-> list:
''' Return the newest update of accounts added.'''
key_list = client_to_accounts.keys()
for i in key_list:
if i == valid_client:
last = len(client_to_accounts[i][0]) - 1
client_to_accounts[i][0].insert(last,balance)
client_to_accounts[i][1].insert(last,interest)
return client_to_accounts[i]
param1 = {('Habib', 696969696): [[1.0, 10000.0], [2.0, 1.0]], ('Hendiye', 123456789): [[20000.0,
-100.0], [1.0, 1.0]]}
param2 = (('Hendiye', 123456789),40.0, 2.0)
param3 = 40.0
param4 = 2.0
actual = savings_account(param1, param2, param3, param4)
print(actual)
输出:
[[20000.0, 40.0, -100.0], [1.0, 2.0, 1.0]]
来自 shell
所以,存在值错误:)
我有以下程序:
def savings_account(client_to_accounts: Dict[Tuple[str, int],
List[List[float]]], valid_client: Tuple[str, int],
balance: float, interest: float)-> list:
''' Return the newest update of accounts added.'''
key_list = client_to_accounts.keys()
for i in key_list:
if i == valid_client:
last = len(client_to_accounts[i][0]) - 1
client_to_accounts[i][0].insert(last,balance)
client_to_accounts[i][1].insert(last,interest)
return client_to_accounts[i]
当我从原始文件中调用这个函数时:
actual = savings_account({('Habib', 696969696): [[1.0, 10000.0], [2.0, 1.0]],
('Hendiye', 123456789): [[20000.0, -100.0], [1.0, 1.0]]},
('Hendiye', 123456789),40.0, 2.0)
print(actual)
#printed: [[20000.0, 40.0, -100.0], [1.0, 2.0, 1.0]]
('Hendiye', 123456789)
的值将正确更新。但是当从其他 python 文件调用此函数时 ('Hendiye', 123456789)
的值不会更新。
import banking_functions
param1 = {('Habib', 696969696): [[1.0, 10000.0], [2.0, 1.0]], ('Hendiye', 123456789): [[20000.0,
-100.0], [1.0, 1.0]]}
param2 = (('Hendiye', 123456789),40.0, 2.0)
param3 = 40.0
param4 = 2.0
actual = banking_functions.savings_account(param1, param2, param3, param4)
#expected = [[20000.0, 40.0, -100.0], [1.0, 2.0, 1.0]]
print(actual)
#printed : [[20000.0, -100.0], [1.0, 1.0]]
您的第二个代码片段调用函数的方式与第一个代码片段不同。
您的意思是:
import banking_functions
param1 = {('Habib', 696969696): [[1.0, 10000.0], [2.0, 1.0]], ('Hendiye', 123456789): [[20000.0,
-100.0], [1.0, 1.0]]}
param2 = ('Hendiye', 123456789) # Just a tuple of two items
param3 = 40.0
param4 = 2.0
actual = banking_functions.savings_account(param1, param2, param3, param4)
#expected = [[20000.0, 40.0, -100.0], [1.0, 2.0, 1.0]]
print(actual)
现在输出与第一个相同。
肯定存在值错误,您的代码在这两种情况下给出了相同的答案。
来自文件:
from typing import List,Dict,Tuple
def savings_account(client_to_accounts: Dict[Tuple[str, int],
List[List[float]]], valid_client: Tuple[str, int],
balance: float, interest: float)-> list:
''' Return the newest update of accounts added.'''
key_list = client_to_accounts.keys()
for i in key_list:
if i == valid_client:
last = len(client_to_accounts[i][0]) - 1
client_to_accounts[i][0].insert(last,balance)
client_to_accounts[i][1].insert(last,interest)
return client_to_accounts[i]
param1 = {('Habib', 696969696): [[1.0, 10000.0], [2.0, 1.0]], ('Hendiye', 123456789): [[20000.0,
-100.0], [1.0, 1.0]]}
param2 = (('Hendiye', 123456789),40.0, 2.0)
param3 = 40.0
param4 = 2.0
actual = savings_account(param1, param2, param3, param4)
print(actual)
输出:
[[20000.0, 40.0, -100.0], [1.0, 2.0, 1.0]]
来自 shell
所以,存在值错误:)