python3-krakenex icon indicating copy to clipboard operation
python3-krakenex copied to clipboard

Why set default value of dictionary parameters to None, but then change them to {} at beginning of method?

Open derekmahar opened this issue 1 year ago • 1 comments

In several API methods like krakenex.api._query, why do you set the default value of some dictionary parameters to None, but then at the beginning of the method, immediately change None to the empty dictionary {}? Why not instead set the parameter default values to {}?

For example, why not replace

def _query(self, urlpath, data, headers=None, timeout=None):
        if data is None:
            data = {}
        if headers is None:
            headers = {}

with

def _query(self, urlpath, data={}, headers={}, timeout=None):

?

derekmahar avatar Mar 19 '24 13:03 derekmahar

Looks like an atavism, judging by commits 839b9a1e and 11ac129a...

veox avatar Jul 01 '24 19:07 veox

i think the suggested alternative -> def _query(self, urlpath, data={}, headers={}, timeout=None): would be problematic

By doing this, the headers and data objects are created at the time of defining the function. All function calls will then share the same object. I think the code is good as is and the issue can be closed, unless there is something i am missing

cloro7 avatar Sep 22 '25 14:09 cloro7

By doing this, the headers and data objects are created at the time of defining the function. All function calls will then share the same object. I think the code is good as is and the issue can be closed, unless there is something i am missing

I agree. At the time I opened this issue, I wasn't aware of this unfortunate Python behaviour. I can see how it could be a source of confusion for programmers new to Python.

derekmahar avatar Sep 23 '25 00:09 derekmahar