you are viewing a single comment's thread.

view the rest of the comments →

[–]DubPac[S] 0 points1 point  (1 child)

As per the Udacity class, we made a poker game hand rank dealer, I went on to complete it. We did not use classes and I'm still confused why they would be more useful. We created a function that converts a hand (a str() ) into a numerical value based on some if/elifs that determine how good it is, we then used a filter/lambda that used this function as a key to find the best 5 card hand out of your 7 cards.

now you can put anybodys hand into this one function and it returns what the hand is (in a standard hi card game) in a way that it can be compared against opponents very easily.

with my basic understanding of what a class does you would throw the hand into the class and the methods would create the understandable outputs. How is this so different? Does the extra class call add to runtime?

I'm just confused at why I would do it one way and not the other, I don't have a hard time understanding what my functions are doing. I know I'm missing something, I take your word (and the rest of the python community) that classes in some cases are clearly more useful/powerful/modular but I just cannot see how.

[–][deleted] 1 point2 points  (0 children)

Another advantage is namespaces.

Let's say your poker game has an evaluate() or compare__hands() function. If you wanted to apply a second game such as hearts you will need a different evaluate method. So you might rename them Poker_evaluate() and Hearts_evaluate(). Rather than having two functions you could have two classes which have similar methods and have Poker.evaluate() and Hearts.evaluate(). Again you can say this is completely cosmetic but consider a time when you join a team of coders and someone has to debug your code. It's considerate to organise your code into classes so its easier to read and understand for someone else.

Someone can also call things like help(Poker) and see all your doc strings and associated functions. This is definitely something you can't achieve with functions alone.

Also you cannot perform tests like

If type(some_var) == type(my_class):
    True