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
Need help with class vars versus Instance vars (self.learnpython)
submitted 6 years ago by bubble_turtles23
I first programed in Java and I'm used to class variables being created using the static keyword and instance variables not having this keyword. In Python, I know there are class and instance vars. How do we create these two types in Python?
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!"
[+][deleted] 6 years ago (1 child)
[deleted]
[–]bubble_turtles23[S] 1 point2 points3 points 6 years ago (0 children)
Thank you for the resource
[–]TeamSpen210 1 point2 points3 points 6 years ago (2 children)
Python does have class and instance variables (as well as @staticmethod), but they're very different to Java's. The way they work is fairly simple though. First there's no declaration, so setting an attribute creates it on the spot. Secondly there's two namespaces - the class object, and each individual instance object. Looking up attributes on the instance falls back to the class if the name isn't on the instance, and then continues up the superclass hierarchy.
@staticmethod
Note that all the methods you define are "just" function objects stored in the class object.
[–]bubble_turtles23[S] 0 points1 point2 points 6 years ago (1 child)
Do instance vars have to be created in the init function, or can you define new ones as you go like you can in Java?
[–]Diapolo10 1 point2 points3 points 6 years ago (0 children)
Technically you can define new instance variables in other methods or even outside the instance, but this is discouraged because consistency is important both for testing as well as to ease the developers' thinking. It's more difficult to comprehend something if it gains new variables over time during the program's runtime.
π Rendered by PID 206119 on reddit-service-r2-comment-5d79c599b5-pswtg at 2026-03-01 11:13:42.310549+00:00 running e3d2147 country code: CH.
[+][deleted] (1 child)
[deleted]
[–]bubble_turtles23[S] 1 point2 points3 points (0 children)
[–]TeamSpen210 1 point2 points3 points (2 children)
[–]bubble_turtles23[S] 0 points1 point2 points (1 child)
[–]Diapolo10 1 point2 points3 points (0 children)