Using the same method name for two different method headers
I'm experiencing a little issue, while working with Python/Django. I have a class, called 'Plantao', and a method 'get_ultima_posicao', which, I wanted to behave in the following way:
If called by the class, like Plantao.get_ultima_posicao, it should expect two parameters, one mandatory and one optional. And when called by a class object, like p.get_ultima_posicao (p being an instance of Plantao) the only parameter it should expect would be self.
Is there a way of doing that?
I know there's a way of doing something like polymorphism, using (self, *args) in the method header, but since one of my methods should be static, using @staticmethod, it doesn't have self as a parameter.
EDIT
In [4]: Plantao.get_ultima_posicao(empresa = 1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/*************************************/<ipython console> in <module>()
TypeError: unbound method get_ultima_posicao() must be called with Plantao instance as first argument (got nothing instead)
In [5]: planta.get_ultima_posicao()
Out[5]: 0
Where planta.empresa = 1. I want开发者_Go百科ed that to work, and both executions returning the same values
I wanted the methods definitions to be something like:
def get_ultima_posicao(self):
AND
@staticmethod
def get_ultima_posicao(empresa, data = datetime.date.today()):
EDIT
Ok, here's my model for the Plantao class:
usuario = models.ForeignKey(Usuario, null=True, blank=True) # user
empresa = models.ForeignKey(Empresa, null=True, blank=True) # department
posicao = models.IntegerField(default=0) # position in queue
data = models.DateField(null=True, blank=True) # date added in queue
So, I may know which deparment I want to get an user from, so why would I need to retrieve one Plantao object just to search for it? At the same time, if I already retrieved it, why would I need to get the deparment again, if it's already in Plantao object? That's why I want to do that.
EDIT
In hope it may help finding a solution I'm adding the functions codes here:
@staticmethod
def get_ultima_posicao(empresa, data = datetime.date.today()):
if empresa is None:
return 'Empresa invalida'
ultima = Plantao.get_ultimo_fila(empresa = empresa)
plantao = Plantao.objects.filter(data=data, empresa=empresa).order_by('posicao')
usuario = None
for i in plantao:
ultima += 1
i.posicao = ultima
i.save()
if i.usuario.atendimento_pendente() == False and i.usuario.cliente_aguardando() == False and i.usuario.esta_online() == True:
return i.usuario
return "Nenhum usuario disponivel na escala"
def get_ultima_posicao(self):
if self.empresa is None:
return 'Empresa invalida'
ultima = Plantao.get_ultimo_fila(empresa = self.empresa)
plantao = Plantao.objects.filter(data=self.data, empresa=self.empresa).order_by('posicao')
usuario = None
for i in plantao:
ultima += 1
i.posicao = ultima
i.save()
if i.usuario.atendimento_pendente() == False and i.usuario.cliente_aguardando() == False and i.usuario.esta_online() == True:
return i.usuario
return "Nenhum usuario disponivel na escala"
精彩评论