[Tutor] seeking design pattern (rmlibre)
rmlibre at riseup.net
rmlibre at riseup.net
Mon Aug 26 11:55:33 EDT 2019
Cameron's solution is very elegant and simple, I would suggest it unless
you have a strong reason not to.
That said, I have had a use for a similar idea in a real-world case:
--- m.py
def very_expensive_function():
"""make a global s (constant or variable) that is very expensive to
compute"""
global s
s = "expensive computation"
def a():
"""either do something with s or return s as-is"""
global s
return s
"""
In this use-case you'd perform the expensive computation once at the
global level in the imported script. This way it's only run once at
import
"""
very_expensive_function()
--- Interactive Console
>from m import a
>a()
>>'expensive computation'
You could also just import the global s
>from m import s
>s
>'expensive computation'
But in your example, this wouldn't be necessary since s is not expensive
to compute, making Cameron's suggestion shine again. However, you may
want to perform some expensive computation on s in one script or
console, but where the underlying function/class and function/class
state resides and depends on another script. I can see this being
valuable. In that case:
--- m.py
def very_expensive_function(some_input):
"""do some expensive computation with some_input and/or state within
m.py and save it to a global s"""
global s
s = f"very expensive computation with {some_input}"
def a(cheap_function=None):
if cheap_function:
return cheap_function(s)
else:
return s
very_expensive_function("some state within the m.py module")
--- Interactive Console
>from m import a, very_expensive_function
>
>a()
>'very expensive computation with some state within the m.py module'
>
>very_expensive_function(3)
>a()
>'very expensive computation with 3'
"""
a hundred lines of code later, quickly get the value without doing the
very expensive computation again and irrespective of the scope
"""
>a()
>'very expensive computation with 3'
m.py could also be a shared resource between many different modules. In
that case you also may want to use this idea so you don't have to define
s in every module. You could instead define it in just one module, m.py,
which is called by all the others. And since it can also just be
calculated once at import, it automates the process of creating a
constant or variable which is unique to each instantiation (such as
creating a random number seed for a PRNG, or getting date based data).
On 2019-08-25 01:47, tutor-request at python.org wrote:
> Send Tutor mailing list submissions to
> tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
> Today's Topics:
>
> 1. Re: venv questions (Mats Wichmann)
> 2. Re: Fwd: Mailing list chaos. (David L Neil)
> 3. seeking design pattern (bob gailer)
> 4. Re: Fwd: Mailing list chaos. (David Rock)
> 5. Re: seeking design pattern (Cameron Simpson)
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list