0

I have a list of id's, that I got from database insertion, to send to other functions in other modules. I have a main module say asd.py and I want to share a list id_list with the functions in other two modules : foo.py and bar.py . This is simply:

in asd.py

def asd():
    id_list = list()
    # Some insertion then append id_list
    foo.f1(id_list)
    bar.f1(id_list)

My question is id_list is copied by value or reference, assume that is a very big list. And is this approach "performance-wise" ?

Thank you.

TheSoulkiller
  • 979
  • 8
  • 11
  • 1
    performance-wise you might want to [cache the list in memory](http://web2py.com/books/default/chapter/29/04/the-core#cache) and avoid loading the list when it's not used. Maybe not all functions in your controllers need your list. In that case you can try [conditional modules](http://www.web2py.com/books/default/chapter/29/04#markmin_conditional_models), or simply loading the data on demand. Hope this helps to answer the second part of your question. – Remco Oct 24 '15 at 00:01
  • @Remco thank you, I didn't look at them. – TheSoulkiller Oct 27 '15 at 07:33
  • @Remco: These are conditional *models*, not modules. – Stefan Steinegger Oct 29 '15 at 14:45
  • @StefanSteinegger indeed, my apologies for the wrong word and thanks for pointing out Stefan. – Remco Oct 30 '15 at 16:01

1 Answers1

2

Id_list is copied by reference, although mutable objects are a weird edge-case in this regard. If the foo.f1 call executes id_list = 7, the copy of id_list in the asd function will not change. However, if foo.f1 calls id_list.append(9), a 9 will appear at the end of id_list in the asd function call. There is no implicit copying of the list.

pppery
  • 3,731
  • 22
  • 33
  • 46