Search code examples
pythonfunctionclassobjectself

Classes vs Function: Do I need to use 'self' keyword if using a class in Python?


I have a data engineering program that is grabbing some data off of Federal government websites and transforming that data. I'm a bit confused on whether I need to use the 'self' keyword or if it's a better practice to not use a class at all. This is how it's currently organized:

class GetGovtData():

    def get_data_1(arg1=0, arg2=1):
       df = conduct_some_operations
       return df

    def get_data_2(arg1=4, arg2=5):
       df = conduct_some_operations_two
       return df

I'm mostly using a class here for organization purposes. For instance, there might be a dozen different methods from one class that I need to use. I find it more aesthetically pleasing / easier to type out this:

from data.get_govt_data import GetGovtData

df1 = GetGovtData.get_data_1()
df2 = GetGovtData.get_data_2()

Rather than:

from data import get_govt_data

df1 = get_govt_data.get_data_1()
df2 = get_govt_data.get_data_2()

Which just has a boatload of underscores. So I'm just curious if this would be considered bad code to use a class like this, without bothering with 'self'? Or should I just eliminate the classes and use a bunch of functions in my files instead?


Solution

  • If you develop functions within a Python class you can two ways of defining a function: The one with a self as first parameter and the other one without self.

    So, what is the different between the two?

    Function with self

    The first one is a method, which is able to access content within the created object. This allows you to access the internal state of an individual object, e.g., a counter of some sorts. These are methods you usually use when using object oriented programming. A short intro can be fund here [External Link]. These methods require you to create new instances of the given class.

    Function without self

    Functions without initialising an instance of the class. This is why you can directly call them on the imported class.

    Alternative solution

    This is based on the comment of Tom K. Instead of using self, you can also use the decorator @staticmethod to indicate the role of the method within your class. Some more info can be found here [External link].

    Final thought

    To answer you initial question: You do not need to use self. In your case you do not need self, because you do not share the internal state of an object. Nevertheless, if you are using classes you should think about an object oriented design.