[英]calling same function with different types in list
I have a list of dictionaries where I am traversing them on by one.
我有一个字典列表,我要逐一遍遍它们。 eg.
例如。
def same_func(messages):
for message in messages:
#message is a dict here.
Now I also want to call this function in a different scenario where my messages is a list of tuples (dict and flag)
现在,我还想在其他情况下调用此函数,其中我的消息是元组列表(dict和flag)
so suppose I have made this list like - 所以假设我把这个清单列为-
messages.append((message, flag))
and now I want to call the same function like same_func(messages) Then how can I make the same_func generic for both scenarios. 现在我想调用相同的函数,例如same_func(messages),然后如何使这两种情况的same_func通用。
why not add a if statement in your for loop that determines the type and then handles the dict or tuple based of that. 为什么不在您的for循环中添加if语句来确定类型,然后基于该类型处理dict或tuple。
ie: 即:
def same_func(messages):
for message in messages:
if type(message) is dict:
# handle dict
elif type(message) is tuple:
# handle tuple
else:
# do something else
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.