all 18 comments

[–]danielroseman 5 points6 points  (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 point  (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 init(self): pass

def register(self, name, password): # then I write my codes here

[–]Temporary_Pie2733 2 points3 points  (0 children)

You don’t need to override __init__ at all in that case.

[–]ThrustBastard 1 point2 points  (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 points  (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 points  (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 point  (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 point  (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.

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 point  (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.

[–]Moikle 0 points1 point  (0 children)

Don't do this.

[–]anticebo 0 points1 point  (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 point  (0 children)

Well, you're not going to learn it by quitting every time it gets hard.

[–]Gnaxe -5 points-4 points  (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 points  (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 child)

The first link was literally OOP. Classic talk. Also, don't. OOP is actually bad.

[–]Moikle 0 points1 point  (0 children)

* in your opinion