How to use self parameter, @staticmethod keyword inside a class and its methods
Asked Answered
M

1

19

I have a python class which has multiple methods. I have defined my methods via @staticmethod instance and I want to call other methods of my class from inside my main function(main_function). I think I need self parameter for calling my other functions from my main function and I want to pass this parameter to my main_function when I create an instance of my class.

class myclass:
  @staticmethod
  def function1(param1)
      print "function1"
  @staticmethod
  def main_function(self, param1)
     function1(param1)

my_object = myclass()
my_object.main_function(param1)

I got this error:

TypeError: main_function() takes exactly 2 arguments (1 given)

The problem is that I have not self parameter when I create my instance. I tried to remove @staticmethod keyword from my method definition and remove all self parameter using, but this does not work.

Murray answered 30/5, 2017 at 8:58 Comment(9)
Why are you using a class at all then? Don't use classes as namespaces, use modules for that.Aggappe
no, my program is a big one and I need modularity in my code.Murray
Yes, that's what modules are for.Aggappe
Classes are for creating custom objects that bundle data and functionality. If you don't have state, don't use classes.Aggappe
A static method is precisely one which does not take self because it does not need to call other instance methods, or would do so via the class name.Ansell
I think I have good reasons for myself to use classes in my code and I suggest we try to solve my problem :DMurray
Don't use @staticmethod, get self, done. You'll have to outline why that doesn't work for you.Ansell
You do not have problem, static methods are not supposed to call instance methods. Change the design and why are you so obsessed with static method? As they don't have cls or self, so precisely they are of no use in most cases.Carmen
@Stateless: you haven't shown any reasons so far, so we can't help you there. Without reasoning on your side, I'll just rely on my 20 years of Python development experience instead and tell you that you should use modules, not classes.Aggappe
A
54

Only use @staticmethod if you are creating a function that you'd normally want to tie to specific classes but do not need any other context. For example, the str.maketrans() function is a static method because it is a utility function you'd often use when working with strings, namespacing it to the already-existing str type (which pre-exists as a class) makes sense there.

You appear to be using classes as a namespace instead. Don't do that. Use a module for your functions, and you don't have to worry about the special scoping rules that apply to classes. Only use a class when you need to bundle state with functionality.

If you insist on using classes with static methods anyway, you are stuck with hardcoding the class name everywhere:

class myclass:
    @staticmethod
    def function1(param1)
        print "function1"

    @staticmethod
    def main_function(param1)
        # Want to use other functions in this class? Then you will
        # have to use the full name of the class as a prefix:
        myclass.function1(param1)

You could make use of classmethods instead so you have a reference to the class object:

class myclass:
    @staticmethod
    def function1(param1)
        print "function1"

    @classmethod
    def main_function(cls, param1)
        # Now you can use the `cls` reference to access other attributes
        cls.function1(param1)

This has the added advantage that you can use inheritance.

However, using a module is the correct way to organise a set of functions into a namespace. Put everything into a my_module.py file in your package, and use importing;

import my_module

my_module.main_function(param1)

Now all globals in my_module are bundled into one module object, and no prefixing or cls references are needed.

Aggappe answered 30/5, 2017 at 9:9 Comment(1)
@Martijin, this is a very good answer. This helped me so much and I understood staticmethod and module via this. Thank you.Murray

© 2022 - 2024 — McMap. All rights reserved.