Python3和递归类 [英] Python3 and recursive class

查看:88
本文介绍了Python3和递归类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义自己的树状类。我已经编写了这样的代码:

 类林:
类树:
def __init __(self ):
self.var_a = []
self.var_b = []
#或简单的
def mk(self,something):
#某些指令
self.a = tree()
b_var = self.a.mk(a_var)
self.b = tree()
c_var = self.a.mk(b_var)
#某些指令
返回ret
#森林类的某些代码

Well tree()不起作用。

  NameError:未定义全局名称'tree'

self.tree的错误( )

  AttributeError:'tree'对象没有属性'tree'

我不知道如何(或是否)使用 self .__ init __ self .__ new __



问题

在Python3中可以使用递归类吗?

解决方案

您无需嵌套类即可实现容器模式



移动 Forest 以外的课程。
每次实例化一棵树,它都可以将自己添加到森林中:

 类森林:
def __init __(self):
self.mytrees = []
def add(self,tree):
self.mytrees.append(self)
def drop_leaves(self): self.mytrees中的树的
.mytrees:
tree.drop_leaves()


类树:
def __init __(self,forest):
forest.add(self)
self.var_a = []
self.var_b = []
#或者简单的
def drop_leaves(self):
print'Drop'

sherwood = Forest()
t1 =树(sherwood)
t2 =树(sherwood)
sherwood.drop_leaves()

问题:


是否可以在Python3中使用递归类?代码
看起来如何?


直接回答:



嵌套类定义在Python中没有任何好处,因为它们的作用域不嵌套(内部类的内容不能直接引用封闭类)。



因此,Python中通常的模式是使两个以上的类比可以直接相互引用的类更多(使用合成而不是继承)。



后续评论:


嗯。我不需要使用类。函数和词典就足够了。


函数和词典就足够了(Python的早期版本没有类)。 OTOH,我们发现类是组织代码的便捷方法,可以清楚地说明哪些函数对哪些数据进行操作。

稍后的评论:


嵌套类有一个好处。它的定义不属于
全局范围。


这也可能是一个缺点,使其更难重用代码,测试起来更加困难,并且可能会混淆自省工具。


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:

    class Forest:
            def __init__(self):
                self.mytrees = []
            def add(self, tree):
                self.mytrees.append(self)
            def drop_leaves(self):
                for tree in self.mytrees:
                    tree.drop_leaves()


    class Tree:
            def __init__(self, forest):
                forest.add(self)
                self.var_a = []
                self.var_b = []
                #Or something as simple
            def drop_leaves(self):
                print 'Drop'

    sherwood = Forest()
    t1 = Tree(sherwood)
    t2 = Tree(sherwood)
    sherwood.drop_leaves()

The question:

It is possible to use recursive class in Python3? How does the code for this look like?

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:

Well. I don't need to use classes. The functions and dictionaries would be enough.

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:

There is one benefit of nested class. Its definition doesn't reside in global scope.

That can be a disadvantage as well, making it more difficult to reuse code, more difficult to test, and possibly confounding introspection tools.

这篇关于Python3和递归类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆