Using self-free method in Python

Asked

Viewed 689 times

2

I saw in other topics some discussions about passing the self as argument. But it was not very clear to me.

Even though my function has no argument to be started I still need to pass the self as a first argument?

For example:

class calculadora:
    def somar(n1,n2):
        return n1+n2
    def subtrair(n1,n2):
        return n1-n2

c=calculadora()
print(c.somar(5,4))   

This class doesn’t need construtor(__init__) to start no argument but the compiler says :

Typeerror: sum() takes 2 positional Arguments but 3 Were Given

I see no basis for using the self in the method somar(), but it is asked. For this specific case above there is some way to use the method somar() without passing the self for him?

2 answers

5


If you want to create a method then you need the self. The object itself will be passed as part of the arguments of the method, so it needs some parameter to receive it, and it is the self.

If you only want one function, and in many cases it’s just what you want, including this example case of the question then don’t use the self, but also do not call as method, ie do not create an object to perform this function. In fact not even create the class, after all classes exist to define objects and there is no object being defined in this code, just create functions.

Then you are correct in not having the use of self there, but there is also no foundation to create a class to do something so simple and without state to be preserved and without a clear consistency about its use. Produces the same result without difficulties:

def somar(n1, n2):
    return n1 + n2
def subtrair(n1, n2):
    return n1 - n2

print(somar(5, 4))

I put in the Github for future reference.

You can put these functions into one module and segregate as you seem to want, that’s the right thing to do. Even if you can avoid creating classes only with static methods or in normal classes where it doesn’t make sense to be there. Nothing indicates in your case that you should use this.

5

You can do what you want, but do it when you know what you’re doing. As Maniero replied, as he put it, it seems more that you only need two functions, so review the need for a class.

However, in Python, unlike some other languages, the class is nothing more than a context management tool, that is, it is acceptable that you use a class only to package the functions within an isolated scope so that there is no conflict with the rest of the application. For this you need to use static methods.

class Calculadora:

    @staticmethod
    def somar(a, b):
        return a + b

Thus, being a static method, you do not need to instantiate the class - the function ("method") belongs to the class, not to the instance.

>>> Calculadora.somar(1, 2)
3

But the same context management you get through the modules. By creating, for example, a file calculadora.py and define the functions in it:

# calculadora.py

def somar(a, b):
    return a + b

And in the application just import the module, calling the desired functions:

>>> import calculadora
>>> calculadora.somar(1, 2)
3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.