Hi there. I'm a New York City based web developer documenting my journey with React, React Native, and Python for all to see. Please follow my dev.to profile or my twitter for updates and feel free to reach out if you have questions. Thanks for your support!
Today's lesson is a simple one yet presents an important milestone in the Object-oriented programming journey. It closely follows yesterday's tutorial on creating classes.
When creating a class in Python, you can either create a variable on the class or you can create an instance variable within the constructor function.
The difference is that class variables are defined on the entire class. When they're changed anywhere they are changed for every instance of the class. This makes them similar to a global variable, and very dangerous.
There may be rare instances where we want to create these, but in general in the wild you'll want to stick to instance variables 99% of the time. These are variables defined only on an instance of a class. When you change them for one instance of a class it doesn't have any effect on the other instances of that class.
In code it would look like this:
class Person:
name = "James"
class Dog:
def __init__(self):
self.name = "Fido"
If you like projects like this and want to stay up to date with more, check out my Twitter @stonestwebdev, I follow back! See you tomorrow for another project.
Top comments (0)