Mick
Mick

Reputation: 1561

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

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?

Upvotes: 3

Views: 6904

Answers (5)

Mark
Mark

Reputation: 92460

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!')

Upvotes: 4

ex4
ex4

Reputation: 2438

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.

Upvotes: 1

user3369545
user3369545

Reputation: 421

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

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

Upvotes: -1

CozyCode
CozyCode

Reputation: 504

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.

Upvotes: 1

Albert Alonso
Albert Alonso

Reputation: 656

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')

Upvotes: 1

Related Questions