2

Python Pandas global vs passed variable

large_dict = {
               'blah1': {'also': 1},
               'blah2': {'big': 1}
             };

def do_something(k):
    for key,val in large_dict[k].iteritems();
        print "%s - %s" % (key,val)


do_something('blah1')
do_something('blah2')

As you can see large_dict is being accessed from the global namespace.

  • is there any advantage of passing in large_dict into do_something as an argument or just access it as a global variable?
  • Related question said local variable is faster but what about memory differences for global vs function argument esp if larg_dict is large (in Perl, we would pass in a reference, not a whole dict)?
Community
  • 1
  • 1
ealeon
  • 12,074
  • 24
  • 92
  • 173
  • Local variables are faster to be looked up, but the size of the object that lookup finds is irrelevant. – jonrsharpe Sep 29 '15 at 14:26
  • @jonrsharpe im talking about the size of object that are being passed into a function. python function memory allocation has no prob with that? Im asking because im not sure how function allocates memory for its arguments – ealeon Sep 29 '15 at 14:27
  • 1
    A function *doesn't* allocate memory for its arguments, calling a function doesn't create a copy. See e.g. http://stackoverflow.com/q/986006/3001761 – jonrsharpe Sep 29 '15 at 14:28
  • 2
    Python only passes references (but passes them by value: [source](http://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/)) so the size of the object passed is always the size of a reference. – Tom Rees Sep 29 '15 at 14:30
  • @jonrsharpe ah okay so then it would be better to pass in as an argument. I wonder if there are any case where it would be better as a global variable – ealeon Sep 29 '15 at 14:30
  • 1
    @ealeon: It's almost never better as a global variable. Globals (especially if they're modified) mean your function isn't thread safe or reentrant, make it run (slightly) slower, and otherwise give up 50+ years of improvements in programming language features for the mild convenience of not having to type the name to pass when you define and call the function. – ShadowRanger Sep 29 '15 at 14:48
  • @ShadowRanger right, i completely understand that part. im saying if there is ever a case where it is better. if not, why allow globals in the first place? – ealeon Sep 29 '15 at 14:53
  • @ealeon: Globals are used for a ton of perfectly safe things, largely involving immutable (or treated as immutable) stuff. When you do `import foo` or `from foo import bar` at the top level of your file, that's creating the globals `foo` and `bar` respectively. Function definitions, classes, modules, etc. are almost always accessed as globals (methods typically are not, typically being accessed as attributes of a class instance, though there are exceptions; the `random` module creates a `Random` instance and exposes bound methods of that instance as module globals for instance). – ShadowRanger Sep 29 '15 at 15:15
  • @ealeon: For short one-off scripts, globals can also be used as a shortcut to avoid defining a class when you'd only create a single instance of the class anyway; it's slightly bad form, but not all scripts need to be production grade. Python tries to straddle the line between scripting language and production grade programming language, and supports both design patterns. – ShadowRanger Sep 29 '15 at 15:17

0 Answers0