python 在python中声明空的类成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1925246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:21:58  来源:igfitidea点击:

declaring empty class member in python

pythonvariables

提问by Draetsch

i am trying to read a tree structure file in python. I created a class to hold the tree objects. One of the members should hold the parent object. Since the parentObject member is of the same type as the class itself, I need to declare this as an empty variable of type "self".

我正在尝试在 python 中读取树结构文件。我创建了一个类来保存树对象。其中一个成员应该持有父对象。由于 parentObject 成员与类本身的类型相同,因此我需要将其声明为“self”类型的空变量。

How do I do that in python?

我如何在 python 中做到这一点?

Thank you very much for your help.

非常感谢您的帮助。

回答by danben

In Python, you don't declare variables as having any type. You just assign to them. A single variable can be assigned objects of different types in succession, if you wanted to do so. "self" is not the type but a naming convention used to refer to the current object, like "this" in Java / C++ (except in Python you could call it anything you wanted).

在 Python 中,您不会将变量声明为具有任何类型。你只需分配给他们。如果您愿意,可以连续为单个变量分配不同类型的对象。“self”不是类型,而是用于引用当前对象的命名约定,就像 Java / C++ 中的“this”(除了在 Python 中,您可以随意称呼它)。

回答by ddaa

Since the parentObject member is of the same type as the class itself, I need to declare this as an empty variable of type "self".

由于 parentObject 成员与类本身的类型相同,因此我需要将其声明为“self”类型的空变量。

No, you do not need to declare anything in Python. You just define things.

不,您不需要在 Python 中声明任何内容。你只是定义事物。

And self is not a type, but the conventional name for the first parameter of instance methods, which is set by the language to the object of the method.

而 self 不是类型,而是实例方法的第一个参数的约定名称,由语言设置为方法的对象。

Here's an example:

下面是一个例子:

class Tree(object):

    def __init__(self, label=None):
        self.label = label
        self.parent = None
        self.children = []

    def append(self, node):
        self.children.append(node)
        node.parent = self

And here's how that could be used:

以下是如何使用它:

empty_tree = Tree()

simple_tree = Tree()
simple_tree.append(Tree('hello'))
simple_tree.append(Tree('world'))

回答by Alok Singhal

I assume that for the root node, you want such an object. It's much more Pythonic to say:

我假设对于根节点,您需要这样一个对象。更 Pythonic 的说法是:

self.parent = None

in your root object, than to create an empty object of type self.__class__. For every other node object in the tree, self.parentshould refer to the actual parent node.

在您的根对象中,而不是创建一个类型为 的空对象self.__class__。对于树中的每个其他节点对象,self.parent都应该引用实际的父节点。

Remember, in Python you don't have 'variables', you have objects, and there is no need for self.parentto be of same type in all the instances of a class.

请记住,在 Python 中,您没有“变量”,您有对象,并且不需要在self.parent类的所有实例中都具有相同的类型。