简体   繁体   English

如何知道function参数值是否是python中的默认值

[英]How to know if a function parameter value is the default in python

I have a function with some parameters that have default values, how can i know when one of those parameters values was changed without having to evaluate each of them individually?我有一个 function,其中一些参数具有默认值,我如何知道其中一个参数值何时更改而不必单独评估它们中的每一个?

This is the solution i have so far that works, i was just wondering if there was a cleaner way of doing it, it haves a list of the default values inside the function and evaluates if the parameters values have changed from default:这是我到目前为止有效的解决方案,我只是想知道是否有更简洁的方法,它在 function 中有一个默认值列表,并评估参数值是否已从默认值更改:

def get_list_movies(self, session, *, limit='20', page='1', quality='All', minimun_rating='0', query_term='0', genre='All', 
                                                sort_by='date_added', order_by='desc', with_rt_ratings='False'):
        opt_attrs = locals()
        opt_attrs.pop('self')
        opt_attrs.pop('session')

        query_string = 'list_movies.json'
        url = ''.join([self.yts_api_url, query_string])
        params = {}
        default_values = ['20', '1', 'All', '0', '0', 'All', 'date_added', 'desc', 'False']

        for index, (k, v) in enumerate(opt_attrs.items()):
            if v != default_values[index]:
                params[k] = v
        print(params)

here default value is assigned by using = that is keyword=value lets understand by different ways there is function jorge ALonso and there is 3 argument out of which 3 argument are assigned by default values so the function jorge alonso accept one argument that is require and other two are default that is optional you got the answer i think if not then i can explain more这里默认值是通过使用 = 分配的,即关键字=值让我们通过不同的方式理解有 function jorge ALonso 并且有 3 个参数,其中 3 个参数被默认值分配,因此 function jorge alonso 接受一个需要和其他两个是默认的,是可选的你得到了答案我想如果没有那么我可以解释更多

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM