This method:
def caminho_junta_posicao(self,d):
p = self.caminho[-1]
self.caminho = self.caminho + [p.posicao_relativa(d)]
doesn't explicitly return
anything, so the result of calling it is None
. Therefore,
caminho(posicao(0,0)).caminho_junta_posicao('este')
will give None
, and None
doesn't have a caminho_origem()
method, hence your error.
Python convention is usually that methods which act in-place (like .append
, .extend
, and here your .caminho_junta_posicao
) return None
, which makes chaining like this impossible. On the other hand, it makes accidentally modifying the original object in a chain which you think is working on copies much harder. Sometimes chaining does come in handy, though (see the pandas
library, for example, which makes extensive use of it to great benefit.)
I don't recommend doing this, but if you modified the method to return self
at the end, i.e.
def caminho_junta_posicao(self,d):
p = self.caminho[-1]
self.caminho = self.caminho + [p.posicao_relativa(d)]
return self
then the result of caminho(posicao(0,0)).caminho_junta_posicao('este')
would be your (now-modified) caminho
object, and you could chain it the way you tried.