Back to articles
January 11, 2025
classes, methods, OOP

OOP Class Planning

Before you decide to define a new class, there are a few things to keep in mind, and questions you should ask yourself:

  • Figure out your data: (Data about songs from iTunes? Data about tweets? Data about hashtag searches on Twitter? Two numbers that represent coordinates of a point on a 2-dimensional plane?)
  • What your class instances represent determines what you name the class:
    • In other words, which sort of new thing in your program should have fancy functionality? One song? One hashtag? One tweet? One point?
    • The answer to this question should help you decide what to call the class you define.
  • What information should each instance have as instance variables? This is related to what an instance represents. See if you can make it into a sentence. 
    • “Each instance represents one song and each song has an artist and a title as instance variables.” 
    • Or, “Each instance represents a Tweet and each Tweet has a User and a content string as instance variables.”
  • What instance methods should each instance have? What should each instance be able to do?
    • Does each song has a method that uses a lyrics API to get a long string of its lyrics
    • Does each song has a method that returns a string of its artist’s name?
    • Or for a tweet, does each tweet has a method that returns the length of the tweet’s message?
  • What should the printed version of an instance look like? (This question will help you decide how to write the __str__ method.)
    • “Each song printed out will show the song title and the artist’s name.”
    • Or “Each Tweet printed out will show the username of the person who posted it and the message content of the tweet.”

After considering those questions and making decisions about how you’re going to get started with a class definition, you can begin to define your class.

Loading comments...