From the course: Python Data Science Mistakes to Avoid

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Using class variables vs. instance variables

Using class variables vs. instance variables - Python Tutorial

From the course: Python Data Science Mistakes to Avoid

Start my 1-month free trial

Using class variables vs. instance variables

- [Instructor] A common mistake to avoid when using object oriented programming is incorrectly using class variables instead of instance variables. It's important to ensure that class variables and instance variables are used properly. I'll be walking through an example in this video. Let's say that I'm designing an online game and I've started defining the class named, player. And my goal is to define it in such a way that each instance of the player class corresponds to a player of the game and contains relevant information for that player. As you can see, word bank is created as a class variable, which is the right choice as all players must have access to that word bank and they cannot change the word bank. Next, points is created as another class variable and is supposed to keep track of the corresponding player's points. Then alive is created as a class variable and is supposed to keep track of whether the player is alive.…

Contents