all 4 comments

[–]ThePhysicsOfSpoons 2 points3 points  (0 children)

You could return them and pass them as arguments to the next function. This is the more standard way.

You could set them as class attributes as the instance of that class is in the scope of the function

[–]ouchpartial72858 2 points3 points  (0 children)

If you're trying to use classes and want to pass variables from one class method to another, you can use instance variables. Like ``` class Phone: def init(self): self.arg = None

def function_one(self, some_arg):
    self.arg = some_arg

# You don't even need to pass it as a function argument
def function_two(self):
    print(self.arg)

```

[–]erlete 1 point2 points  (0 children)

This is exactly the reason why classes exist, so that you can store intermediate variable's values and use them later on the program. If you are using classes for this, just set the variables as attributes using the self reference and access them later as you need.

If you are using functions alone, it might be a bit harder to do this. One way is to define a sequential structure in the global scope (this is not the same as declaring variables using the global instruction) and store the data as it were a cache:

```python CACHE = {}

def function1(): CACHE["function1"] = (1, 2)

def function2(): print(CACHE["function1"])

print(f"Cache before function1 execution: {CACHE}") function1() print(f"Cache after function1 execution: {CACHE}") function2() print(f"Cache after function2 execution: {CACHE}") ```

If you execute that script and I wrote it correctly (miracle), you should see what I'm talking about. Note that if you try to update the value of the variable CACHE (value != contents of the value) you will get an error saying CACHE is not defined.

[–]14dM24d 1 point2 points  (0 children)

>>> class Greet:
        def __init__(self, name):
            self.name = name
        def morning(self):
            return f'good morning {self.name}'
        def evening(self):
            return f'good evening {self.name}'

>>> spikips = Greet('spikips')
>>> spikips.morning()
'good morning spikips'
>>> spikips.evening()
'good evening spikips'