python store variable in function and use it later

前端 未结 5 1504
星月不相逢
星月不相逢 2021-02-06 18:01

is it possible to store a variable from a while loop to a function and then call that same variable from the function at the end of the loop

for eg:during while loop, p

相关标签:
5条回答
  • 2021-02-06 18:36

    No, you can't do this.

    Also, it's a terrible, terrible idea. "Store to a function" is such an awful and wrong thing to do that I hesitate to provide working code.

    Use a callable object.

    class Store( object ):
        def __init__( self ):
            self.x, self.y, self.z = None, None, None
        def __call__( self, x=None, y=None, z=None ):
            if x is None and y is None and z is None:
                return self.x, self.y, self.z
            else:
                self.x, self.y, self.z = x, y, z
    

    What's better is to do something simpler that doesn't involve a function with magical properties that does two different things when called with and without arguments.

    Anything is better than "store to a function". Anything.

    Really.

    Anything.

    0 讨论(0)
  • 2021-02-06 18:38

    Technically speaking, if you had a deep, burning desire to do this with functions, you can always do it with a closure (which is, as we all know, a poor man's object):

    def store(a,b,c):
        def closure():
            return (a,b,c)
        return closure
    
    stored = store(1,2,3)
    print stored()
    

    prints in (1,2,3)

    0 讨论(0)
  • 2021-02-06 18:41

    Hmmm, unless I am misunderstanding, this is a classic non-solution to a non-problem.

    Why not just use the language as it is?

    while condition:
    
       x = something
       y = else
       z = altogether
       ...
       save_state = (x,y,z)   ## this is just a python tuple.
       ...
       # do something else to x, y and z, I assume
       ...
       x, y, z = save_state
    

    Depending on the type of x, y and z you may have to be careful to store a copy into the tuple.

    (Also, your indentation is wrong, and there is no such thing as end in python.)

    Update: Ok, if I understand better, the question is just to be able to use the previous value the next time through. In the simplest case, there is no problem at all: the next time through a loop, the the value of x,y, and z are whatever they were at the end of the previous time through the loop (this is the way all programming languages work).

    But if you want to be explicit, try something like this:

     x_prev = some_starting_value
     x = some_starting_value
     while condition:
          x = something_funky(x_prev)
    
          .... other stuff ....
    
          x_prev = x
    

    (but again, note that you don't need x_prev at all here: x=something_funky(x) will work.)

    0 讨论(0)
  • 2021-02-06 18:53

    As much as this is a bad idea I like applying weird solutions so here

    class Store(object):
        def __init__(self, f):
            self.f = f
            self.store = None
    
        def __call__(self, *args):
            self.store = self.f(*args)
    
    @Store        
    def test(a,b,c):
        return a+b+c
    
    print test(1,2,3)
    print test.store
    

    Decorate the function with Store then call function.store to get what you called in it, if the function is never called it returns a single None, you could make it raise an exception but personally I didn't want to.

    0 讨论(0)
  • 2021-02-06 19:01

    As others have stated, there is probably a more suitable choice than this, but it might be convenient in some situations (perhaps in a REPL). Here's a simple function that does what you want with any number of values.

    def store(*values):
        store.values = values or store.values
        return store.values
    store.values = ()
    

    Example

    >>> store(1, 2, 3)
    >>> a, b, c = store()
    >>> print a, b, c
    1 2 3
    >>> store(4, 5)
    >>> a, b = store()
    >>> print a, b
    4 5
    
    0 讨论(0)
提交回复
热议问题