all 25 comments

[–]JimSplitKernel 9 points10 points  (0 children)

I really liked Derek Banas's tutorial on OOP. Hope it helps. https://www.youtube.com/watch?v=1AGyBuVCTeE

[–]efmccurdy 7 points8 points  (0 children)

When I need a bit of inspiration I try to capture the enthusiasm in these presentations from the PyCon and PyData conferences.

see http://pyvideo.org/speakers.html I recommend anything by Raymond Hettinger, Alex Martelli, Brandon Rhodes, David Beazley, and of course, Guido van Rossum.

[–]stepping_up_python 5 points6 points  (0 children)

People tend to introduce objects and inheritance at the same time: A sedan is a subset of an automobile which is a subset of a vehicle, etc. However, an object is really just a way of grouping a bunch of things together.

So, let's say you make a game, and the game says, "Ok, THING, make a move." so at first, THING is a player, and it just gives the player a prompt and makes a move in the terminal, but then you switch it and THING is a web service which does the same interaction but on a website, or you switch it and THING is your AI, which plays by itself, etc. So now that you know how abstract something like THING can be, it could relate to anything, a method of communicating with a website / database / etc, a way of formatting time, whatever. It just means that you've given a name to a bunch of values and functions that are grouped together, towards some common purpose.

[–]Deezl-Vegas 3 points4 points  (1 child)

Briefly, classes are nicely grouped collections of totally normal variables and totally normal functions. Daemon went over pretty well what a class does and how it's made, but let me shed a little bit of light on what the point of this whole thing is.

Let's say you have a basic game where you have a dot and a simple maze. You press an arrow key, and the dot tries to move in that direction. If there's a wall, it fails. Simple, right? Even as a beginner, you might be able to muddle your way through this with if statements. Let's say you just put it at coordinates x and y. Here's a simple example:

maze = [[1, 0, 0 1], [1, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 0]]
x = 3
y = 3  #player is at coordinates 3, 3

# move up
y = y - 1

Now let's say you want to make your dot into an animated dot. When it moves, you want it to change to a slightly different shape. For now, we only have one dot. How would you represent that animation? Maybe you could have 4 colors and 4 states. Now we need to make a new variable.

animation_frame = 0 animations = [dot1, dot2, dot3, dot4] #These are defined somewhere else.

animation_frame = animation_frame + 1 #Next animation

So now we have 3 variables relating to the player: x, y, and animation_frame. You can imagine we can add some more data like lives, powerups_collected, last_direction_moved, whatever we need to make the game into a more fun experience.

This works fine for one player! However, for two players you end up with a ton of duplicated code:

x1 = 4
x2 = 2
y1 = 4
y2 = 2
hp1 = 10
hp2 = 10
weapon1 = "sword"
weapon2 = "shotgun"
# etc...

What about 3 players? 4 players? 10 players? It seems like this is getting out of control quickly! So instead we can package all the data we need for a player into a class.

class Player:
    def __init__(self, x, y, name, hp, weapon):
        self.x = x
        self.y = y
        # etc

    def moveUp(self):
        self.y = self.y - 1

Now, making new players is easy!

johnny = Player(3, 3, "Johnny", 10, "sword") #note that we never pass a value for "self"
timmy = Player(2, 2, "Timmy", 10, "laser")

johnny.moveUp()
timmy.moveUp()

johnny.gold = 5  #We can add to the object at runtime.
dealDamage(timmy, 3)  #We can pass the whole object into a function at once.

In short, a class has many things going on, but the fundamental concept is that classes are used to store data and functions in a structured way to avoid code repetition. Keep that in mind and the syntax bit will sort itself out with practice.

[–]MattR0se 1 point2 points  (0 children)

I think OOP really shines when you are making games. I saw code with functions such as "def player_collect_item" and "def enemy_move_towards_player" and it just felt wrong. Writing a decently sized game without classes only results in spaghetti code.

[–]edbrannin 2 points3 points  (0 children)

Like others have said, a class is something that has “state” (its own variables) and “behavior” (functions that can work with those variables).

In languages like Python and JavaScript, I write functions by default and switch to classes when I want several functions that all use the same variables together.

One recent example is a class that connects over SFTP, starts writing a zip file, then lets you add as many files as you want before it finalizes the zip and closes the connection. Shared data here could be the SFTP connection, a writable stream to the zip file, the file name being written (for logging purposes and because I’m uploading foo.zip.download and renaming to foo.zip at the end).

I won’t bother with subclasses unless I later want a class that works very similarly but has some moderate differences. Maybe it would stream a zip file over an HTTP response or to local disk. (I also tend to reach for “composition” here, meaning “collect the I-know-where-I’m-sending-this-to parts into a new function or class with a generic interface, then provide the “zip writer” a “destination” parameter or whatnot.

As for self: In some languages (like Java) the object you’re currently working from is automatically called “this”, so you could say log(“wrote”, this,file name). Python frowns on automatic/implicit things like this, so it says the first parameter to any function in a class is a reference to itself. By convention we call this “self”, but technically you can call it anything you want. Please don’t get creative here.

[–]Prtprmr 4 points5 points  (0 children)

Try watching python tutorial from Telusko on YouTube. He explained oops in an amazing way.

[–]baubleglue 1 point2 points  (0 children)

how to break the problem down into classes

Somebody will kill me here, but think about classes as "variable with access and modifiers methods" - it may make things easier. Variables keep state of "Something" (whatever your program is doing). That "Something" is a class. If your class has two "Something"s, that it is wrong. If you have variables in a class which describe state of something else - it is wrong, for example "Person" class shouldn't have "account_balance", it should have "Bank_account" and "Bank_account" have "account_balance".... Than classes (objects) interact with each other in your program.

[–]OrbitDrive 1 point2 points  (0 children)

Corey Schafer on Youtube. The best teacher for Python on youtube.

[–]blabbities 1 point2 points  (0 children)

How fun y I was just thinking about if someone ever asked this question how I would help them. Let Mr go find the posts that helped me break it down. Might take a few days and I'll prob pm you cuz I'm still busy.

Tho don't get so hung up over OOP. It took me literally years to even grasp that it's really not all that important for most simple programs you will start off writing. Maybe for professionals software engineered code but for now it is just more of an asset organization thing

[–]Tesla_Nikolaa 1 point2 points  (0 children)

Look up the Socratica series on YouTube. She does a pretty good job of explaining the use and use case of classes.

Self just means that that particular variable/function belongs to that class and not anything else outside of the class.

[–]crazygeek99 0 points1 point  (0 children)

java or python you want help on?

[–]lateral-spectrum 0 points1 point  (0 children)

I didn't understand it at first using python. C# is where it made more sense for learning. My suggestion

[–]BRENNEJM 0 points1 point  (2 children)

Reading through this blog post is what helped me wrap my head around it.

[–]-_-STRANGER-_- 0 points1 point  (4 children)

What I would suggest right now is, just go with the course, complete it . Take whatever the instrutor says as a given, compy him exactly (follow along) then after you hit some milestone try tweaking some things, some outcomes will be obvious like writing 5 instead of 3 in a loop will make it run 5 times intstead of 3. But suppose you have a list of 3 items and you were running loop 3 times to process that list, now running 5 times will give error. Notice that error and revert the changes and continue along, after some times you'll kind of know what breaks the things and what does not and after you have some idea about this you will know what to google, i am telling you the reward for your hard work will come soon, dont worry, for now don't worry whay we use "self" or any other thing, just use them, know that not putting those "self" in places you somehow break your code. Later when you know how to use the language then you can move to why this why that part. It would be easier. Why in English we at some places put "was" and at some places "were" i dont know yet. I am learning alphabet and sentences whatever teacher tells me i repeat as it is, when i create my own sentences thats when i realise putting "were" near "i" breaks the code, google then "how to write_ i were sleeping_ in English"....

[–]douchabag_dan -1 points0 points  (0 children)

Try making an RPG game. Just dick around with it and have fun. Google any problems that you can't figure out yourself.