How to Store the Result of an Executed Function and Re-Use Later

How to store the result of an executed function and re-use later?

def readDb():
... #Fetch a lot of data from db, spends a lot time
return aList

def calculation(data):
x=data
...process x...
return y

data = readDb()

calculation(data)
calculation(data)
calculation(data)

This will only hit the database once.

Basically, you want to save the results of readDb() to a seperate variable which you can then pass to calculation().

Execute a function and use the output later in my script without calling the function again - Javascript

Store the result of deal() on a variable and reuse the variable instead of calling it again (unless if you want to do calculation again)

How to store/stash JavaScript event and reuse it later?

After reading your question a few times, and the answers another few,

The question: how can any javascript Object be stored with its methods?

The answer: there is no how.

However,

Josh properly explained you can extract and store all the serializable properties, say data, from your event.

I will just add you can create an event with somehow that same data later anywhere, this new event will have all the methods any Event has, but by now probably none of use.

Obviously, even serialized in your data, properties like timeStamp, isTrusted, etc... will be overriden at creating the new event.

What you just miss / need is an EventTarget, the value of the event.target property,
the reference which is lost forever when document.body unloads forever, or when serializing the event Object.

But if it is still alive, or if you know what event.target should be, like a DOM Element or any Object you can reference, from wherever you recreate the event (where?), just dispatch your event to that object, if it listens to that event.type,
your brand new event should be at least heard.

Simple example from MDN EventTarget, or see EventTarget.dispatchEvent

As a comment over the extensive answer by cegfault: eval, and text source code... could be <script> text source code </script>... should your script produces a (String) script. If not you ´d probably better go further backwards to where did your script creates the unserializable things that appear in your event, and think about recreating those things, not the event.

How can I store a result of a function in a variable instead of just printing it?

Generally, you return the value. Once you've done that, you can assign a name to the return value just like anything else:

def randombb():
return random.random()

a = randombb()

As a side note -- The python tutorial is really great for anyone looking to learn a thing or two about the basics of python. I'd highly recommend you check it out. Here is the section on functions: https://docs.python.org/2/tutorial/controlflow.html#defining-functions ...

Reuse function output in if-else condition without temporary

As of yet, what you want is not possible because the python ternary operator only allows expressions and not statements. If you are curious about the difference I would recommend reading this amazing answer on the topic. But the important part for this question is that what you are asking for is an assignment (i.e. something like tmp = fooA(), as you correctly noted yourself), which is a statement.

Luckily, other people like you (and me) would appreciate the option to share temporary results within a single block, expression, or statement, which is the goal of PEP 572, due for python3.8 in late 2019. Given the PEP gets accepted.

You can try it already by compiling the POC from source. This is what it would look like:

>>> my_func = lambda: 'foo'
>>> tmp + 'bar' if (tmp := my_func()) == 'foo' else tmp
foobar
>>> tmp + 'bar' if (tmp := my_func()) == 'baz' else tmp
foo

Which is imo prettier and more straight forward than what you currently have to do with temporary variables:

>>> tmp = my_func()   # store the temp var
>>> if tmp == 'foo': # the test
... tmp += 'bar' # the optional change
>>> tmp # printing/reassigning the temp var
foobar


Related Topics



Leave a reply



Submit