简体   繁体   中英

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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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