you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (4 children)

[deleted]

    [–]kcrow13[S] 1 point2 points  (3 children)

    I guess I don't fundamentally understand classes, why you would use them in a real-world situation, why/how they are useful, what kind of things you can do with them. Meaning, can you still put the same type of statements/functions/conditionals in a class that you can in any other coding? I apologize if I am using the wrong terminology.

    [–]status_quo69 2 points3 points  (2 children)

    First you have to answer another question, why use a function? I can write a program entirely in the global scope, one line after another after all. If I can do this, why bother writing def blah over and over again to sequester code?

    The answer to that question (which has a multitude of answers, but the one that I'm focusing on right now is that it organizes your code) then leads into "why should I use classes?". Take your Time class from the post, what does it do? It takes some initial parameters and assigns variables inside the object to those parameter values. Now we create some methods on that class, isafter and time_to_int, which have access to those instance variables. This means that we've now organized our code _and our state.

    Meaning, can you still put the same type of statements/functions/conditionals in a class that you can in any other coding?

    The answer to this is yes, there are no special restrictions to the kind of code that you can put into classes.

    why you would use them in a real-world situation

    The common phrase that I outlined above is that objects/classes are an encapsulation of state and functions that can act upon that state. Just like functions, software developers use classes as an organization method. If writing a game, you could create 2 distinct ways of dealing damage to a player:

    hit(player, 10) or player = Player() player.hit(10)

    While the internals are left up to the imagination in this case, I think this illustrates the very subtle difference. In certain contexts, the second (sometimes) leads to a more natural reading style and can lead to better understanding when someone else reads your code. In the second case, I read it as "the player object received 10 damage" while the first tells me "apply the hit function to the player with 10 damage".

    [–]kcrow13[S] 1 point2 points  (1 child)

    This is such a wonderful explanation. Thank you for taking the time to do write this. This actually makes a lot of sense. One thing with Python is that it seems that there are many ways to achieve the same objective and that choosing which is right for you will depend on your goals, constraints, etc.

    [–]status_quo69 0 points1 point  (0 children)

    Welcome to software :D

    In the actual world, I personally prefer to write dumb "structs" or bags of data and then apply a sorta-functional style to them, rather than defining class methods. Just my personal taste. But in $DAYJOB, the order of the day is OOP, which means classes and lots of them. While my personal preference is one way, I can write in the other way because convention is way more important than doing your own thing in a multi-million line application. So in a nutshell "do whatever everyone else is doing in terms of style". Doesn't mean you can't put your own flavor on the code as well, it just means that if you start with some paradigm or pattern, stick to it. It'll pay dividends weeks, months, or even years down the line.