Access to the caller's globals, not your own

Rob Gaddi rgaddi at highlandtechnology.invalid
Thu Nov 17 13:12:44 EST 2016


Steven D'Aprano wrote:

> On Thursday 17 November 2016 04:52, Rob Gaddi wrote:
>
>>> import library
>>> result = library.make_spam(arg)
>>>
>>>
>>> versus:
>>>
>>> import library
>>> make_spam = library.make_library()
>>> result = make_spam(arg)
>>>
>>> What a drag.
>>>
>>>
>> 
>> And there you have it; an entire extra line at import time.
>
> Multiplied by every function that has a configurable default. Multiplied by 
> every module that imports any of those functions, *whether or not* they change 
> the default. If there are four functions, the user has to do this:
>
> import library
> make_spam = library.make_spam_factory()
> make_eggs = library.make_eggs_factory()
> make_cheese = library.make_cheese_factory()
> make_toast = library.make_toast_factory()
>
>
> before they can even begin to do what they actually want to do, which is make 
> spam, eggs, cheese etc. And yes, there's likely to be four or more of these 
> functions.
>

No, no no no.  You misunderstood the approach that I think most of us
were on, which is

  import library
  Library = library.Library()
  Library.SPAMPARAM = 'jam'

  i_want_spam_now = Library.make_spam(99)

One extra line for the import, plus one for each default setting they
want to override (which you're stuck with either way).  I suppose you
could even pass all those overrides in to the Library __init__, but now
we're in the weeds.

And again, for the ones that don't want to mess with the default params,
you can have:

  import library.defaultinstance as Library


-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list