I have a relatively straightforward question but couldn't really find the answer online. I know in C++, if you are trying to create a large object (say a huge vector of strings) you would avoid doing:
int main() {
vector<string> v = foo()
}
vector<string> foo() {
vector<string> result;
// populate result
return result;
}
instead opting for the version below because a method call like above will have to copy the local vector to the vector declared in main whereas the lower example adds directly to the vector in main (more or less):
int main() {
vector<string> v;
foo(v)
}
void foo(vector<string>& result) {
// populate result
return result;
}
My question is the same related to python. Is it better to pass in a mutable object as a parameter or have the method create one locally and return it? I'm not incredibly familiar with the mechanics of Python.