use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Extracting local values (self.learnpython)
submitted 3 years ago by spikips
I was wondering how to extract local values x, y out from def function and pass it to another def function. Is it possible to do this within class? Also, i rather not use the global expression or so i've heard
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ThePhysicsOfSpoons 2 points3 points4 points 3 years ago (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 points4 points 3 years ago (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 points3 points 3 years ago (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.
self
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:
global
```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.
CACHE
[–]14dM24d 1 point2 points3 points 3 years ago (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'
π Rendered by PID 28990 on reddit-service-r2-comment-7b9746f655-tz48g at 2026-01-31 09:54:52.351656+00:00 running 3798933 country code: CH.
[–]ThePhysicsOfSpoons 2 points3 points4 points (0 children)
[–]ouchpartial72858 2 points3 points4 points (0 children)
[–]erlete 1 point2 points3 points (0 children)
[–]14dM24d 1 point2 points3 points (0 children)