Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 711
Stateless Avatar asked May 30 '17 08:05

Stateless


People also ask

Can you use self in a class method?

By using the “self” we can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes.

Can I use self in static methods?

Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls . Static methods don't have access to cls or self . They work like regular functions but belong to the class's namespace.

How can we use static method inside class?

A static method doesn't have access to the class and instance variables because it does not receive an implicit first argument like self and cls . Therefore it cannot modify the state of the object or class. The class method can be called using ClassName. method_name() as well as by using an object of the class.

Can I use this keyword inside a static method?

In order to call a static method or property within another static method of the same class, you can use the this keyword.


1 Answers

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.

like image 90
Martijn Pieters Avatar answered Oct 07 '22 19:10

Martijn Pieters