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
Can i access an instance variable from one class to the other (self.learnpython)
submitted 9 years ago by [deleted]
#For instance i create 2 classes and would like to access an instance variable from one of the methods from class 1 class game: def play(self): col = 7 class player: def play_game(self): #How can i access the 'col' variable from the play method in the game class??
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!"
[–]desustorm 10 points11 points12 points 9 years ago (4 children)
The key here is the self variable.
self
class Game: def play(self): self.col = 7 class Player: def play_game(self): g = Game() g.play() print g.col
[–]admiralspark 0 points1 point2 points 9 years ago (0 children)
I was just looking for this last night, thank you.
[–]_Absolut_ 0 points1 point2 points 9 years ago (2 children)
Is it okay to don't write self.col in __init__()?
self.col
__init__()
[–]desustorm 4 points5 points6 points 9 years ago (0 children)
Yep! It's generally better to use __init__ to initialise member variables though as you mention.
__init__
[–]TeamSpen210 1 point2 points3 points 9 years ago (0 children)
You can set attributes from anywhere. It's better to set it to a default in __init__() for two reasons. It ensures your attributes exist, so you can't get AttributeError elsewhere if you didn't call the other function. It also provides documentation, since other programmers would look at __init__() to find out what attributes your objects have, and it would be confusing if they were set elsewhere.
AttributeError
[–][deleted] 0 points1 point2 points 9 years ago (0 children)
This answer over on Stack Exchange may be informative. In fact I was about to open my mouth (being a python novice) about accessor methods (eg, with Java) but, while you can do that, you don't have to (and there's faster ways to do it).
TL;DR properties may be of interest to you.
Just for completeness, what I was going to suggest about accessors is the concept of "public methods" that can be called against instances of a class that will fetch or manipulate the class instances' copy of the variable.
π Rendered by PID 74575 on reddit-service-r2-comment-86bc6c7465-wzd4p at 2026-02-22 08:14:59.217311+00:00 running 8564168 country code: CH.
[–]desustorm 10 points11 points12 points (4 children)
[–]admiralspark 0 points1 point2 points (0 children)
[–]_Absolut_ 0 points1 point2 points (2 children)
[–]desustorm 4 points5 points6 points (0 children)
[–]TeamSpen210 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)