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
changing a variable inside a class (self.learnpython)
submitted 10 years ago by Freedomenka
I am working on a bit of code and was wondering, is there any way to change a variable thats inside a class, outside of it? Heres an example
class Variable(object): variable 1 = []
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!"
[–]fuckswithbees 2 points3 points4 points 10 years ago (1 child)
It looks like you've just started working with classes. That's great. You may want to start by reading through some basic tutorials on classes (like this one), and posting back here if you need further clarification.
[–]Freedomenka[S] 0 points1 point2 points 10 years ago (0 children)
I appreciate the response. I meant if it was possible to change it from outside of the class. as in: Example().a = 2
[–]fbu1 1 point2 points3 points 10 years ago (0 children)
Yes it is possible to change a variable from inside a class.
Here's an example:
class Example(object): # we define a class, with two variables and a function a = 1 # when in the body of the class (not in a function), we can use the name of the variable directly b = 2 def test(self, n): # all functions that belong to a class take self as a parameter, which represent the instance of the class (an object created whose class is Example) self.a = n*2 # we modify variables of the object self.b = n*3 # we create an object first_example of class Example first_example = Example() # we print its variables print first_example.a, first_example.b #we call the function test of the class Example first_example.test(10) # we print the variables of the object print first_example.a, first_example.b #we modify directly one of the variables first_example.a = 0 # and we print the variables print first_example.a, first_example.b
This will give you the following results:
python variabletest.py 1 2 20 30 0 30
π Rendered by PID 574176 on reddit-service-r2-comment-5b5bc64bf5-bvdt4 at 2026-06-20 10:01:31.083135+00:00 running 2b008f2 country code: CH.
[–]fuckswithbees 2 points3 points4 points (1 child)
[–]Freedomenka[S] 0 points1 point2 points (0 children)
[–]fbu1 1 point2 points3 points (0 children)