Python根据创建的文件函数返回不同的变量类型



我下面有这个函数,async def get_all_distinct_identifications(),如果我在CarelinePeopleService类中创建它,await CareLinePeopleService().enrolled_people_list_in_care_lines()函数的返回将是一个元组,而不是熊猫DataFrame,这是它应该真正返回的。我甚至可以将CareLinePeopleService()更改为self,它仍然返回元组而不是DataFrame

但是,如果我在另一个文件中创建这个函数async def get_all_distinct_identifications,它将返回所期望的结果,即一个Pandas Dataframe。

我认为这是Python在幕后由于某种原因搞砸的。

有人知道发生了什么事吗?

async def get_all_distinct_identifications(insurer_id, trigger_type, conditional_type, conditionals_fields):
df_conditionals = await ConditionalsService().get_from_conditional_table(
insurer_id=insurer_id,
trigger_type=trigger_type,
conditional_type=conditional_type,
fields=conditionals_fields,
care_lines=[],
)
df_of_people_identified = await CareLinePeopleService().enrolled_people_list_in_care_lines(
df_care_line=df_conditionals[['care_line_id']].drop_duplicates().reset_index(drop=True),
identifications=None,
limit=1000,
offset=None
)
return df_of_people_identified

CareLinePeopleService()的定义。enrolled_people_list_in_care_lines是:

async def enrolled_people_list_in_care_lines(self, df_care_line, identifications=None, limit=1000, offset=None):
if type(offset) == int:
df_people_identified = await self.get_batch_of_enrolled_people_by_care_line_id(df_care_line, limit, offset)
elif identifications is not None:
df_people_identified = await self.get_new_enrolled_people_in_care_lines(identifications)
else:
df_people_identified = await self.get_all_enrolled_people_in_care_lines(df_care_line)
return df_people_identified

以上三个函数的定义:


async def get_batch_of_enrolled_people_by_care_line_id(self, df_care_line, limit, offset):
list_of_care_line_id = list(df_care_line['care_line_id'].drop_duplicates().astype(str))
query = CareLinePeopleRepository.enrolled_people_list_in_care_lines(
list_of_care_line_id=list_of_care_line_id,
limit=limit,
offset=offset
)
df_of_people_identified = await Mysql().execute_to_pd(query)
return df_of_people_identified
async def get_new_enrolled_people_in_care_lines(self, identifications):
list_of_care_line_id = list(identifications['care_line_id'].drop_duplicates().astype(str))
identifications_list = list(identifications[['person_id', 'care_line_id']].drop_duplicates().itertuples(index=False, name=None))
query = CareLinePeopleRepository.enrolled_people_list_in_care_lines(
list_of_care_line_id=list_of_care_line_id,
identifications_list=identifications_list
)
df_of_people_identified = await Mysql().execute_to_pd(query)
return df_of_people_identified
async def get_all_enrolled_people_in_care_lines(self, df_care_line):
list_of_care_line_id = list(df_care_line['care_line_id'].drop_duplicates().astype(str))
query = CareLinePeopleRepository.enrolled_people_list_in_care_lines(
list_of_care_line_id=list_of_care_line_id
)
df_of_people_identified = await Mysql().execute_to_pd(query)
return df_of_people_identified

当你说:

…如果我在CarelinePeopleService类中创建它,…

如果该函数(get_all_distinct_identifications)看起来像它当前所做的,它没有将self作为第一个参数。
由于它没有像@staticmethod这样的装饰,取决于它的调用方式(您没有指定),参数可能不是您认为的那样。
当您通过外部文件(而不是从类中)调用它时,您可能会以不同的方式调用它,并正确地传递参数。

最新更新