Access to the caller's globals, not your own

Antoon Pardon antoon.pardon at rece.vub.ac.be
Wed Nov 16 10:19:02 EST 2016


Op 16-11-16 om 09:36 schreef Steven D'Aprano:
> On Tuesday 15 November 2016 15:55, Dan Sommers wrote:
>
>> On Mon, 14 Nov 2016 16:20:49 +1100, Steven D'Aprano wrote:
>>
>>> import library
>>> SPAMIFY = False  # only affects this module, no other modules
>>> result = library.make_spam(99)
>> I must be missing something, because it seems too obvious:
> [snip]
>
> I wouldn't say "obvious" so much as "complex".
>
> A factory function or a class that holds state for a local make_spam() callable 
> would be a possible solution, but it significantly increases the complexity, 
> especially for the simple case:
>
> import library
> result = library.make_spam(arg)
>
>
> versus:
>
> import library
> make_spam = library.make_library()
> result = make_spam(arg)
>
> What a drag.

What about how the random module solves this?

The random module provides a factory class: Random.

It also provides functions which are methods of a
hidden instantiation.

-- 
Antoon Pardon.




More information about the Python-list mailing list