In Python you can call a method or property from within a two-mode class, but first consider the following class:
class Pessoa:
def __init__(self, nome = "nenhum"):
self.nome = nome
def set_nome(self, nome):
self.nome = nome
def get_nome(self):
return self.nome
- Passing the object to the class when calling the method:
amigos = [Pessoa("Lucas"), Pessoa("Julia"), Pessoa("Bruna")]
for amigo in amigos:
print(Pessoa.get_nome(amigo))
In this method you use the class to call the methods and properties, but the first argument must be an object of this class, as it needs to take this information from somewhere. I usually use this method when I have a list of objects of the same type, because it facilitates access to methods and properties, apparently when reading your question I had the impression that you used this form.
- Calling the method through the object:
maria = Pessoa("Maria")
print(maria.get_nome())
This method is the most widely used and is very similar to what happens in object-oriented languages like Java and C#. So that "Python does not send self
" as described in your question, you need to use this method. Just a correction, you said that order
is a function, if order
is asking as a first argument self
means that it is contained in a class, so it is not called "function" but "method".
That works, but it’s far from the right way. If you have bothered to put a member of the class because you want to reuse it in several test cases, it needs to be a generic callable. If it is an object you have to call the attribute
__func__
, and tomorrow that_VIEW
is switched to something else, has to touch the methods.– jsbueno
@jsbueno I agree. I left a note at the beginning of the reply clarifying this point.
– mgibsonbr