-2

I am trying to understand 'self' in python on a deeper level because while I use it, I find myself not understanding it in full.

Consider the code below

class FoodItems:

def __init__(self, other arguments):
some code

def main():
some code

item1 = FoodItems
item1.main()

For the method [ def main() ] I am not passing the parameter 'Self' in it and the code works when I call it. My understanding of 'Self' is that it has to be passed every time a method is created under the class. If I pass 'Self', and then call the method, I get the error ( missing 1 required positional argument: 'self')

Can some one explain this to me?

Now, the second part of the question, when it comes to creating instances,I cant tell the difference between calling an instance with or without parentheses such as ( item1 = FoodItems Vs. item1=FoodItems() )

Note: When I use parantheses I get the error, missing 8 required positional arguments that have been initialized in the constructor.

Any help would be greatly appreciated, thanks in advance!

  • Your code isn't indented properly, and to create a new `FoodItems` object, you need to go `FoodItems()`. Please could you fix this. – sbottingota Apr 20 '23 at 05:20
  • `item1 = FoodItems` *doesn't* create any instance, period. You’re just aliasing the class to a different name. – deceze Apr 20 '23 at 05:22

1 Answers1

1

In Python, self is a reference to the instance of the class. It's used to access instance variables and call instance methods. When you define a method inside a class, you need to include the self parameter to work with the instance. However, when you call the method, you don't need to pass self explicitly, as Python does it for you.

Regarding your first question, when you don't pass self in the main() method definition, it becomes a static method. Static methods are methods that don't depend on the instance of the class and don't use the self parameter. Here's an example:

class FoodItems:
    def __init__(self, other_arguments):
        pass

    @staticmethod
    def main():
        print("Static method called")

item1 = FoodItems()
item1.main()

In this case, you can call the main() method without passing self. However, you won't be able to access any instance variables or other instance methods within the main() method.

For the second part of your question, the difference between item1 = FoodItems and item1 = FoodItems() is that the first one assigns the FoodItems class itself to item1, while the second one creates a new instance of the FoodItems class and assigns it to item1.

When you use parentheses (item1 = FoodItems()), you're actually calling the constructor init of the class, which initializes a new object. If you get an error about missing positional arguments, it means that you need to pass the required arguments when creating a new instance. For example:

class FoodItems:
    def __init__(self, name):
        self.name = name

    def main(self):
        print("Instance method called")

# Create a new instance of FoodItems with the required arguments
item1 = FoodItems("Apple")
item1.main()

In this case, you need to pass the name argument when creating a new instance of FoodItems. When you call the main() method, Python implicitly passes the instance as the self argument

Please checkout these articles for more background knowledge: Realpython.com blog and the official documentation.

Luis Arteaga
  • 773
  • 8
  • 11
  • 1
    Hi Luis thank you so much for the extensive explanation it is really great of you will hunt down more info in the links provided. Thanks again! – user3768679 Apr 20 '23 at 05:45