DEV Community

Taylor Riley
Taylor Riley

Posted on

Building a Class in Python

Star Wars Gif
Python can be a very powerful language, especially when it comes to databases and backend programming. When dealing with so many bits of data and instances, it makes sense that you might need a way to target and manipulate the data. But, what if you only want to target certain groups, or types of data? Luckily, Python has built-in methods for that called classes. With the help of classes, you can set attributes for each dataset for them to be initialized with and different methods and instances for each class. Building out a class can make your data much more manageable and easier to format your data.

Building out the Class Object

In order to start your class blueprint, you first need to give your class a name. It can be anything but it should be relevant to your class to make your code more readable. For example, if your making a class for a data set of dogs, your class name could be "Dog". When defining a class in Python, it is a best practice to use a capital letter for the class name. Declaring a class for my dogs would look something like this:

class Dog:
Enter fullscreen mode Exit fullscreen mode

Now that we have a class name for the object, we need to decide what attributes we want our objects initialized with. If we keep going with the dog example, we just need to think about what kind of data we want to start with. Again, this can be anything, it is all relative to the purpose our dataset is going to serve. For our example, our dog could have a name and an age. These are all things we could know right off the bat and want our class instances to have when they are created. In order to do this, we are going to use the __init__ function for our class.

class Dog:
    def __init__(self, name, age):
Enter fullscreen mode Exit fullscreen mode

Here, we have passed our attributes we want our class instances to be initialized with. You might have noticed that in addition to name and age, I also passed in the word self. Passing in self just refers to the individual instance that is going to be passed in to be formatted. We need that so we can attach the individual attributes to a given instance like so:

class Dog:
    def __init__(self, name, breed, age):
        self.name = name
        self.age = age
Enter fullscreen mode Exit fullscreen mode

Creating Class Methods

Now that we have initialized our class, we can do a bunch of different things. For example, we could do is create a list of all our dog instances and append our dog instances to it.

class Dog:
    all = []
    def __init__(self, name, breed, age):
        self.name = name
        self.age = age
        Dog.all.append(self)
Enter fullscreen mode Exit fullscreen mode

Using this new list that we've created, we can define methods to do things like return the amount of dog instances we have, or return all the dog objects.

class Dog:
    all = []
    def __init__(self, name, breed, age):
        self.name = name
        self.age = age
        Dog.all.append(self)
    def dogs(self):
        return [dog for dog in Dog.all if dog.dog is self]
    def num_dogs(self):
        return len(self.dogs())
Enter fullscreen mode Exit fullscreen mode

In this example, were returning the list of all the dog instances, then returning the amount of dogs in that list. Using methods in your classes can help you access or manipulate the data specific to your class elsewhere in your document, wherever the class is accessible.

Creating Class Instances

Now that we have class attributes and class methods, we need to come up with a few class instances. Instances are just the individual data of the classes, so for my class of dogs, my instance would be a dog. When we make a new instance, we need to reference what class the instance is a part of and initialize it with all the information defined in our __init__.

class Dog:
    all = []
    def __init__(self, name, breed, age):
        self.name = name
        self.age = age
        Dog.all.append(self)
    def dogs(self):
        return [dog for dog in Dog.all if dog.dog is self]
    def num_dogs(self):
        return len(self.dogs())

    dog1 = Dog("Paco", "Golden Retriever", "3")
    dog2 = Dog("Jeff", "Dachshund", "1")
    dog3 = Dog("Mr. Pickles", "Shih Tzu", "6")
Enter fullscreen mode Exit fullscreen mode

While these are just a few basics to creating classes in python, there are many more things that classes are capable of. Hopefully this post was helpful to you and your coding journey in python.

Resources

Python Classes Documentation

Top comments (0)