python store variable in function and use it later

前端 未结 5 1499
星月不相逢
星月不相逢 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.

提交回复
热议问题