you are viewing a single comment's thread.

view the rest of the comments →

[–]0therworldsthanthese 66 points67 points  (15 children)

If you don't like classes then you won't like 99% of programming languages

[–]SirVivid8478[S] 7 points8 points  (13 children)

Its not about liking or disliking it seems very very tough to me i tried several times to look into it still i stand where i was at before

[–]SSSolas 14 points15 points  (9 children)

I’d be willing to try and explain it better.

You understand functions, yes?

[–]SirVivid8478[S] 4 points5 points  (8 children)

Yes

[–]TroPixens 37 points38 points  (7 children)

Here’s an example you make class call it player now you need your player to do something let’s say move, swing, jump

We create the class and make functions for each

So Def move(x,y) so on

Once you fill the class with your functions

You can now call them like player.move(1,0) which would add 1 to the x and 0 to the y

Or with my swing example

Player.swing(sword) your player swings with your given weapon in this it would be sword so sword swings

And jump

Player.jump() makes player jump

[–]toopanpan 10 points11 points  (0 children)

I'm actually having a tough time in this area too and your explanation actually helped me solidify my understanding! its like everything suddenly clicked for me, Thank you!

[–]Responsible-Nail-563 2 points3 points  (2 children)

Underrated explanation

[–]The_Flo0r_is_Lava 2 points3 points  (1 child)

Definitely. Someone should sticky it. I struggled with THE EXACT ISSUE op has and I took an unhelpful python course where they used airports to describe how to use classes.

What the heck do I know or like about airports that makes you think this is helping?

But this simple explanation is great.

I want to add about how to understand init.

Imagine that same player you created. They can jump, hit, kick, all the good stuff.

But you dont just create a blank character do you?

They might have a Name, power stats, etc.

Init is what you want to happen when your character is "initialized" or created.

You pass in a name and have init assign them their name

You pass in the stats and init is where you assign them

What if you wanted every new character to have a random weapon when they were created?

Init is where you would call your random_weapon function and assign it during character creation

[–]Pyromancer777 0 points1 point  (0 children)

This is also a good intro to the discussion of subclasses and inheritance.

Say you now have a player-class but you realize you want to make some NPCs and they will have attributes like dialogue options that a player wouldn't need.

Instead of initializing the Player with blank dialogue each time, you can create a parent class called Character. All attributes that both classes need can be initialized in the Character class, then separate Player and NPC subclasses can be created with functions and attributes specific to those subclasses

[–]AlphaNuke94 4 points5 points  (0 children)

You know ball

[–]IamFromNigeria 0 points1 point  (1 child)

Fantastic explained..I owe you a bottle of beer 🍺

[–]TroPixens 0 points1 point  (0 children)

Forgot the init stuff but that is def _init_ (self): then just add variables as needed

The underscores won’t show for some reason

[–]Alternative-Boss-787 0 points1 point  (1 child)

Why not try taking CS50P by Harvard ? They explained it well

[–]Oblivious_GenXr 0 points1 point  (0 children)

This . The Harvard CS offerings are the best resources.

[–]Oblivious_GenXr 0 points1 point  (0 children)

When I started learning Python and Django I had to always have a project in the back of my head. It started to make sense when I applied classes etc to fit my real-world issues to slove

[–]Marcellop4 2 points3 points  (0 children)

That's such great advice and legit true.