0

I want to extend the nltk.tree class i.e. :

 class XTree(Tree): 
    ......

What I want is to "cast" this return value to my XTree class, because I don't have control over methods that return Tree objects, so that I can use the additional methods I have in XTree.

I know I can not cast in python, but how would I do what I want ? m'I missing something trivial


After some research I found that there is class method Tree.convert() which does exactly that :)

https://www.nltk.org/_modules/nltk/tree

Anyway it is interesting if there is general solution to the question.

sten
  • 7,028
  • 9
  • 41
  • 63
  • 1
    You want to downcast a `Tree` to an `XTree`? – Carcigenicate Aug 14 '19 at 00:28
  • You could try to just assign a new class to the object at run time: `object.__class__ = XTree` https://stackoverflow.com/questions/15404256/changing-the-class-of-a-python-object-casting – Håken Lid Aug 14 '19 at 00:32

0 Answers0