python - When importing a module in another .py , return of function is different to directly run it?
问题描述
I have the following program:
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]
When I call this func from original file like to:
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]]
the value of ('Hendiye', 123456789)
correctly will be update. but when call this function from other python file the value of ('Hendiye', 123456789)
isn't updated.
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]]
解决方案
Your second snippet does not call the function in the same way the first does.
Did you mean:
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)
Output same as first now.
推荐阅读
- javascript - 试图返回所有对象...存储在 Javascript 中的另一个主要对象中
- apache-kafka - Nifi-1.10.0 在 Kafka-2.x 中消费消息时是否支持事务?
- docker - 可以在没有存储库的情况下使用 jib 吗?
- reactjs - 交叉点oberserver api / useEffect挂钩触发回调两次的问题
- ruby-on-rails - 枚举是否仅适用于 ruby on rails 中的整数字段?
- java - 未触发 JMS 读取器/处理器/写入器步骤后的 Spring 批处理步骤
- salesforce - 当salesforce对象中删除记录时,是否有任何salesforce触发器自动从Azure SQL中删除记录
- tailwind-css - 如何使用顺风手表启动反应应用程序
- django - RoleLeaderSerializer' 对象不可调用?
- android - Android EditText 观察者对 API 调用的时间限制?