<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:12pt"><div id="yiv9320807880"><div><div style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12pt;"><div id="yiv9320807880yui_3_16_0_5_1402747355371_10"><span id="yiv9320807880yui_3_16_0_5_1402747355371_16">ssss</span></div> <div class="qtdSeparateBR"><br><br></div><div class="yiv9320807880yqt0946072731" id="yiv9320807880yqt44638"><div class="yiv9320807880yahoo_quoted" id="yiv9320807880yui_3_16_0_5_1402747355371_13" style="display: block;"> <div class="yiv9320807880yui_3_16_0_1_1402747355371_8943" style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12pt;"> <div class="yiv9320807880yui_3_16_0_1_1402747355371_8944"
style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12pt;"> <div dir="ltr"> <font size="2" face="Arial"> On Friday, 13 June 2014, 12:45, Steven D'Aprano <steve@pearwood.info> wrote:<br clear="none"> </font> </div> <br clear="none"><br clear="none"> <div class="yiv9320807880y_msg_container">On Fri, Jun 13, 2014 at 12:51:25PM +0530, diliup gabadamudalige wrote:<br clear="none">> Hi All!<br clear="none">> Hope everyone is
well.<br clear="none">> <br clear="none">> In my code there are many dictionaries and lists which are used in various<br clear="none">> functions. Is it better/pythonic/efficient to have these inside the<br clear="none">> function itself or declared at the beginning of the program in which case<br clear="none">> they will be global? They are all read only. I understand that global<br clear="none">> constants and variable have memory allocated to them but when declared<br clear="none">> inside a function are created on the fly, used and discarded. Please<br clear="none">> enlighten me further on this and correct me if i'm wrong.<br clear="none"><br clear="none">A good question.<br clear="none"><br clear="none">Depending on the size of these dictionaries and lists, worrying about <br clear="none">efficiency here may be premature optimization. As they say:<br clear="none"><br clear="none"><br clear="none"> "We should forget
about small efficiencies, say about 97% of <br clear="none"> the time: premature optimization is the root of all evil."<br clear="none"> -- Donald Knuth<br clear="none"><br clear="none"><br clear="none"> "The First Rule of Program
Optimization: Don't do it. The <br clear="none"> Second Rule of Program Optimization (for experts only!): <br clear="none"> Don't do it yet." -- Michael A. Jackson<br clear="none"><br clear="none"><br clear="none"> "More computing sins are committed in the name of efficiency<br clear="none"> (without necessarily achieving it) than for any other single <br clear="none"> reason — including blind stupidity." -- W.A. Wulf<br clear="none"><br clear="none"><br clear="none">If these lists and dicts are small, say, fewer than a dozen items, the <br clear="none">time to create and destroy them is probably trivial, especially if <br clear="none">you construct them from constant literals. In that case, it's a <br clear="none">matter of personal taste whether you prefer them as global constants or <br clear="none">local to a function.<br clear="none"><br clear="none">But if it takes a long time to build
the list, then you definitely <br clear="none">should move it outside the function and perform the initialisation step <br clear="none">only once:<br clear="none"><br clear="none"># This may take a while...<br clear="none">PRIMES = [prime(i) for i in range(1,
1000001)]<br clear="none"><br clear="none"><br clear="none">If your lists really are read-only, then you should consider turning <br clear="none">them into tuples:<br clear="none"><br clear="none"># Not this:<br clear="none">SOME_ITEMS = ["this", "that", "another"]<br clear="none"># Better:<br clear="none">SOME_ITEMS = ("this", "that", "another")<br clear="none"><br clear="none"><br clear="none">One advantage of the tuple is that in recent versions of Python, the <br clear="none">tuple may be compiled as a constant instead of being built at runtime. <br clear="none">This is from Python 2.7:<br clear="none"><br clear="none"><br clear="none">py> from dis import dis<br clear="none">py> code = compile("x = [2, 4, 8]", "", "exec")<br clear="none">py> dis(code)<br clear="none"> 1 0 LOAD_CONST 0 (2)<br clear="none">
3 LOAD_CONST 1 (4)<br clear="none"> 6 LOAD_CONST 2 (8)<br clear="none"> 9 BUILD_LIST
3<br clear="none"> 12 STORE_NAME 0 (x)<br clear="none"> 15 LOAD_CONST 3 (None)<br clear="none"> 18 RETURN_VALUE<br clear="none"><br clear="none"><br clear="none">py> code = compile("x = (2, 4, 8)", "", "exec")<br clear="none">py> dis(code)<br clear="none"> 1 0 LOAD_CONST 4 ((2, 4, 8))<br clear="none"> 3 STORE_NAME 0 (x)<br clear="none"> 6 LOAD_CONST 3 (None)<br clear="none">
9 RETURN_VALUE<br clear="none"><br clear="none"><br clear="none"><br clear="none">-- <br clear="none">Steven<br clear="none">_______________________________________________<br clear="none">Tutor maillist - <a rel="nofollow" shape="rect" ymailto="mailto:Tutor@python.org" target="_blank" href="mailto:Tutor@python.org">Tutor@python.org</a><br clear="none">To unsubscribe or change subscription options:<br clear="none"><a rel="nofollow" shape="rect" target="_blank" href="https://mail.python.org/mailman/listinfo/tutor">https://mail.python.org/mailman/listinfo/tutor</a><br clear="none"><br clear="none"><br clear="none"></div> </div> </div> </div></div> </div></div></div></div></body></html>