4 ms·
Better pattern is to follow the style of Numpy or Pandas and other libs (that OP also mentioned, but then failed to apply it in his own examples), letting the c
by nnq 7y ago
Better pattern is to follow the style of Numpy or Pandas and other libs (that OP also mentioned, but then failed to apply it in his own examples), letting the caller/user of your functions choose, if/when they want better performance, to opt-in for in-place modification, like have either:
def normalize(data, inplace=False):
if not inplace:
data = data.copy()
...
or:
def normalize(data, out=None):
if out is None:
out = data.copy()
...
Please, do follow this pattern for any libraries you release, having value-semantics-by-default to prevent shooting yourself in the foot + opt-in in-place mutation option for better memory performance is the right thing, and it's quite easy too with Python, Numpy and Pandas!
- ourlordcaffeine 7y agoJulia has this as well, functions with a '!' at the end mutate the data, functions without make a copy. So you have the functions 'filter' and 'filter!', 'sort' and 'sort!' etc.
- itamarst 7y agoEven if your API provides both choices, you can use the pattern described in the article to make the copying variation more memory efficient.