简体   繁体   English

在 another.py 中导入模块时,返回 function 和直接运行不一样?

[英]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. ('Hendiye', 123456789)的值将正确更新。 but when call this function from other python file the value of ('Hendiye', 123456789) isn't updated.但是当从其他 python 文件调用此 function 时, ('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]]
 

Your second snippet does not call the function in the same way the first does.您的第二个片段不会像第一个片段那样调用 function。

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. Output 现在和第一个一样。

there must be a value error your code gives the same answer in both cases.必须有一个值错误,您的代码在这两种情况下都给出了相同的答案。

from file:从文件:

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)

output: output:

[[20000.0, 40.0, -100.0], [1.0, 2.0, 1.0]]

and from shell从 shell

在此处输入图像描述

so, there is value error:)所以,有值错误:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中导入模块时运行 function - Run a function when importing module in Python 从另一个.py文件导入函数时未定义numpy包 - numpy package not defined when importing function from another .py file 从另一个模块导入功能 - Importing a Function from another module 从另一个模块导入 function 时,命名空间的概念是什么? - What is the concept of namespace when importing a function from another module? 使用Python在其他模块中导入函数的返回值时出错 - Error when importing the returned value of a function in a different module using Python 返回时在函数中导入模块 - Importing module in a function when returning 在模块中导入模块时出现Web.py NameError - Web.py NameError When Importing Module in Module 将main.py导入另一个模块(包含在同一目录中) - Importing main.py into another module (contained in the same directory) TypeError:func()缺少1个必需的位置参数:'self'从discord.py中的不同文件夹导入function时 - TypeError: func() missing 1 required positional argument: 'self' when importing a function from a different folder in discord.py 从导入另一个模块的类的模块导入函数时的Python导入错误(ModuleNotFoundError) - Python Import Error (ModuleNotFoundError) when importing a function from a module that imports a class from another module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM