I want to define own tree-like class. I've written code like this:
class forest:
class tree:
def __init__(self):
self.var_a = []
self.var_b = []
#Or something as simple
def mk(self,something):
#Some instructions
self.a = tree()
b_var = self.a.mk(a_var)
self.b = tree()
c_var = self.a.mk(b_var)
#Some instructions
return ret
#Some code for class forest
Well tree()
doesn't work.
NameError: global name 'tree' is not defined
The error for self.tree()
:
AttributeError: 'tree' object has no attribute 'tree'
I do not know how (or if) to use self.__init__
or self.__new__
in this context.
The question
It is possible to use recursive class in Python3? How does the code for this look like?
You don't need to nest the classes in order to implement a container pattern.
Move the Tree class outside of Forest. Each time a tree is instantianted, it can add itself to the forest:
The question:
Straight answer:
Nesting class definitions does not confer any benefit in Python because their scopes don't nest (the contents of the inner class cannot refer directly to the enclosing class).
Accordingly, the usual pattern in Python is to make two of more classes than can refer directly to one another (using composition rather than inheritance).
Subsequent comment:
Functions and dictionaries are always enough (the early versions of Python did not have classes). OTOH, we've found that classes are a convenient way to organize code, making it clear which functions operate on which data. How you do it is a matter of taste.
A later comment:
That can be a disadvantage as well, making it more difficult to reuse code, more difficult to test, and possibly confounding introspection tools.
I just experimented with this and it can be done in Python3
This way you don't have to pollute global name space with a 'tree' class
Though if you dislike this due to confusion/readibility issues, you can always make a separate tree class as previously described.