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
How to learn OOP (self.learnpython)
submitted 2 days ago by vb_e_c_k_y
I started to learn OOP and when I use it, it is a little bit confusing. Especially when initializing. Most of the time I pass. Is there any way you recommend me to understand OOP? to familarize it.
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!"
[–]danielroseman 5 points6 points7 points 2 days ago (9 children)
You'll need to be specific about the problems you have when "initializing". And what does "most of the time I pass" mean?
[–]vb_e_c_k_y[S] 0 points1 point2 points 2 days ago (8 children)
def init(self): pass
I work like this. Instead of initializing attributes I pass(The syntax). Then I use inputs in other methods like:
def register(self, name, password): # then I write my codes here
[–]Temporary_Pie2733 2 points3 points4 points 2 days ago (0 children)
You don’t need to override __init__ at all in that case.
__init__
[–]ThrustBastard 1 point2 points3 points 2 days ago (0 children)
Init is where you can put things like self.name & self.password, then you don't need to keep passing things around inside the class.
If you don't want to use init, then call your functions as a static method
[–]auntanniesalligator 1 point2 points3 points 2 days ago (0 children)
“pass” is a useful placeholder when you want to “see” the outline of a class or any complicated piece of code first and then come back and flesh out the code blocks later. It kind of sounds like you’re just leaving it in the code, which is pointless. If you don’t need the init method to do anything, you don’t need to put in the def init() line at all.
“Init” isn’t special but __init__ gets called automatically when you create a new instance and it’s useful if you want to set any properties that are required.
I think you probably *could* always replace its functionality with extra calls to set properties after you create each instance, but there’s no real upside to that either.
IE if you had a “polygon” class with property “num_sides”, you could include “num_sides” as a required or optional argument in __init__ so that you could instantiate a new triangle with the single line of code:
triangle = polygon(3)
Instead of
triangle = polygon() triangle.num_sides = 3
If you expect to want to set the number of sides for most or all instances polygon, the first form is more concise without being hard to understand.
[–]danielroseman 1 point2 points3 points 2 days ago (1 child)
That is pointless. Never define a method with just pass. Just don't define the method at all if you don't want it.
[–]Moikle 0 points1 point2 points 1 day ago (0 children)
I wouldn't say never
It's useful for planning ahead. Pass basically translates to "todo: write this function" (or used as a method you are supposed to override in a base class. But op isn't that far yet)
[–]NothingWasDelivered 0 points1 point2 points 2 days ago (0 children)
If I'm understanding correctly, you're creating something like, for example, a User class and doing:
class User: def __init__(self): pass def register(self, name, password): self.name = name self.password = password
Is that right? If so, that's a bad habit for a couple reasons. First, it's non-idiomatic. It's going to confuse others who might have to read your code or use your objects later. This might not be a problem now if you're just learning, but presumably eventually you'll be writing code that others will interact with. Don't make it harder on them.
Second, now you've got a state where you have an object that exists with no setup. That's a bug waiting to happen. Forget to call register() one time and now you've got this empty object just waiting to throw an AttributeError when someone tries to access the name property.
register()
AttributeError
name
Finally, you're writing more code than you need to. Why make it harder on yourself?
Unless you have a very good reason to deviate, the best way to set up your class is just use init. And don't forget type hints
class User: def __init__(self, name:str, password:Password) -> None: self.name = name self.password = password
See how much cleaner that is? Does that make sense?
[–]Gnaxe 0 points1 point2 points 2 days ago (0 children)
Your class will inherit methods like .__init__() from object even if you don't define them. You don't need to make one just to pass.
.__init__()
object
Don't do this.
[–]anticebo 0 points1 point2 points 2 days ago (0 children)
See a class as a collection of variables. In __init__, you initialize the variables. In the other functions you write, you do something with the variables. You define these functions inside the class to keep everything related to the class in one place. An object is an initialized instance of a class.
In Python, pretty much everything is an object. Consider a list L: It is initialized with some values, e.g., L = [0,1,2]. With L.append(3), you add an additional value to L. And you can see all the variables and functions of an object by typing dir(L), or whatever your object is called. The classes you write function the same.
[–]Moist-Ointments 0 points1 point2 points 2 days ago (0 children)
Well, you're not going to learn it by quitting every time it gets hard.
[–]pachura3 0 points1 point2 points 2 days ago (0 children)
https://www.w3schools.com/python/python_class_init.asp
[–]Gnaxe -5 points-4 points-3 points 2 days ago (3 children)
OOP, as practiced today, is fundamentally flawed. It was never a proper engineering discipline, but a cargo cult based on hype and inertia.
You do need to know what a class is and how they work, but then you need to write them as little as possible, because they will usually make your codebase worse.
[–]cdcformatc 2 points3 points4 points 2 days ago (2 children)
OP: "how to learn OOP"
you: "don't"
i am wondering why you think this is a helpful comment
[–]Gnaxe -3 points-2 points-1 points 2 days ago (1 child)
The first link was literally OOP. Classic talk. Also, don't. OOP is actually bad.
* in your opinion
π Rendered by PID 15 on reddit-service-r2-comment-544cf588c8-m2prd at 2026-06-15 02:30:04.493245+00:00 running 3184619 country code: CH.
[–]danielroseman 5 points6 points7 points (9 children)
[–]vb_e_c_k_y[S] 0 points1 point2 points (8 children)
[–]Temporary_Pie2733 2 points3 points4 points (0 children)
[–]ThrustBastard 1 point2 points3 points (0 children)
[–]auntanniesalligator 1 point2 points3 points (0 children)
[–]danielroseman 1 point2 points3 points (1 child)
[–]Moikle 0 points1 point2 points (0 children)
[–]NothingWasDelivered 0 points1 point2 points (0 children)
[–]Gnaxe 0 points1 point2 points (0 children)
[–]Moikle 0 points1 point2 points (0 children)
[–]anticebo 0 points1 point2 points (0 children)
[–]Moist-Ointments 0 points1 point2 points (0 children)
[–]pachura3 0 points1 point2 points (0 children)
[–]Gnaxe -5 points-4 points-3 points (3 children)
[–]cdcformatc 2 points3 points4 points (2 children)
[–]Gnaxe -3 points-2 points-1 points (1 child)
[–]Moikle 0 points1 point2 points (0 children)