How can I call another method of the same class without an instance?

Viewed 367

I am directly calling a method on a class like this:

MyClass.action("Hello World!")

and inside the called method I need to refer to another method:

class MyClass:
    def action(data):
        print('first')
        # vvv How to perform this call?
        next_action(data)

    def next_action(data):
        print('second', data)

Usually, I would use self to access the method and attributes but by calling the method on the class there is no instance that self could refer to. How can I still access one method from another in this case?

6 Answers

Based on how you are calling it, it look like you are trying to define class methods. To do that include @classmethod decorator. It will then pass the class as the first argument, which you can use to call it.

class MyClass:
    @classmethod
    def action(cls, data):
        print('first')

        cls.next_action(data)

    @classmethod
    def next_action(cls, data):
        print('second', data)

MyClass.action('Hello World!')

If, in fact, you are actually trying to make instance methods, then you need to call them from an instance. In that case you define the class without the classmethod decorator and call it from an instance. Python will then pass a reference to the instance as the first argument. But you need to create the instance to call it:

class MyClass:
    def action(self, data):
        print('first')

        self.next_action(data)

    def next_action(self, data):
        print('second', data)

instance = MyClass()
instance.action('Hello World!')

You need to write using the self argument.

class MyClass:
    def action(self, data):
        print('first')

        self.next_action(data)

    def next_action(self, data):
        print('second')

You need to create an instance (object) from your class and call it.

class MyClass:
    def action(self):
        print('first')

        self.next_action()

    def next_action(self):
        print('second')

my = MyClass()
my.action()

Then your methods always has self as a first argument which refers to object itself.

You have to put self as the first parameter of the method as such:

class MyClass:
    def action(self, data):
        print('first')

        self.next_action(data)

    def next_action(self, data):
        print('second')

And the name self is actually just a naming convention, so you can change it to whatever name you want it to be, it just has to be the first parameter. But of course I would advise you to stick with it.

Since these are member functions, call it as a member function on the instance, self.

def next_Action(self,data):
self.next_action(data)

pass self that is the reference of the object

#!/usr/bin/env python3.10

class MyClass:
    def action(self, data):
        print('first')

        self.next_action(data)

    def next_action(self, data):
        print('second')

if __name__ == "__main__":
    myclass = MyClass()
    myclass.action('hmmm')

You can also make the methods as class methods by using @classmethod decorator if you don't want them to instance method

#!/usr/bin/env python3.10

class MyClass:

    @classmethod
    def action(cls, data):
        print('first')

        MyClass.next_action(data)

    @classmethod
    def next_action(cls, data):
        print('second')

if __name__ == "__main__":
    myclass = MyClass()
    myclass.action('hmmm')
Related