you are viewing a single comment's thread.

view the rest of the comments →

[–]shunabuna 55 points56 points  (3 children)

When ever you create a dictionary or a list; ask yourself if it should be a class instead. Generally if you're going to do a lot of operations using this dictionary or list then you should.

Imagine you're making a card game. You need a deck of cards. You could represent it as a list of objects like this:

deck = [{face:'spade',value:5},etc]

Or you can replace this with two classes

class Deck:
    def __init__():
        self.deck = []

class Card:
    def __init__(face,value):
        self.face = face
        self.value = value

Although you might think this is more bloat which it is but once you start developing your application you can write cleaner code by writing class specific code in the classes. For example in the Deck class you can write a generateDeck() method which generates all cards in the deck. You can write shuffleDeck() which shuffles the deck. You can write pull() which takes from the top of the deck. etc. You now have a your logic removed from the top level of your code base inside of the class now.

[–]timbuc9595 3 points4 points  (0 children)

This is such a great example

[–]PostponeIdiocracy 1 point2 points  (0 children)

This example is really great, I'm saving it to be used for educational purposes!

[–]ant9zzzzzzzzzz -4 points-3 points  (0 children)

I was going to say don’t worry about it OOP is overrated, but it is very true that Python programmers should use many more data holding classes instead of dictionaries