Skip to content

python function parameters should not contain mutable objects #28

@alecklandgraf

Description

@alecklandgraf

see: https://github.com/tempodb/tempodb-python/blob/master/tempodb/client.py#L49

def get_series(self, ids=[], keys=[], tags=[], attributes={}):
can cause a bunch of issues since python will not allocate a new list for each get_series call.
would rewrite with:

def get_series(self, ids=None, keys=None, tags=None, attributes=None):
    if ids is None:
        ids = []
...

e.g.

In [78]: def test_params(x=[]):
   ....:         x.append(3)
   ....:         print x
   ....:

In [79]: test_params()
[3]

In [80]: test_params()
[3, 3]

In [81]: a = [1,2,3,4]

In [82]: test_params(a)
[1, 2, 3, 4, 3]

In [83]: test_params(a)
[1, 2, 3, 4, 3, 3]

In [84]: test_params()
[3, 3, 3]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions