python中的结构

    科技2026-08-18  20

    We all know about the structures used in C and C++. The one that allows to bundle multiple primitive data types into user defined data type.

    我们都知道C和C ++中使用的结构。 一种允许将多种原始数据类型捆绑为用户定义的数据类型的方法。

    In python, can define a structure using a class, where the user does not define any functions in the class.

    在python中,可以使用类定义结构,而用户没有在类中定义任何函数。

    Ok, I know that even though C doesn’t, C++ allows function definitions in structure. This is a feature I have never used. I can’t remember making a class with all data members and member functions as public, where it makes sense to use a struct instead of a class.

    好的,我知道即使C没有,C ++也允许结构中的函数定义。 这是我从未使用过的功能。 我不记得创建一个所有数据成员和成员函数都为公共的类,在这种情况下,使用结构代替类是有意义的。

    Structures come very handy to define a complex data type that is formed by using multiple simple primitive data types.

    使用结构轻松定义通过使用多个简单原始数据类型形成的复杂数据类型。

    In Python, there is a way where you can define a collection that mimics properties similar to the structure by using namedtuple under collections.

    在Python中,有一种方法可以定义一个集合,该集合可以通过在collections下使用namedtuple来模仿类似于结构的属性。

    An example below, a collections.namedtuple Point is defined that contains x and y fields.

    下面的示例定义了一个collections.namedtuple Point,其中包含x和y字段。

    from collections import namedtuple Point = namedtuple('Point', ['x', 'y'])

    A similar structure in C++ can be defined as

    C ++中的类似结构可以定义为

    struct Point { float x; float y;}

    Default values for the variables can be set using

    变量的默认值可以使用

    Point = namedtuple('Point', ['x', 'y'], defaults=[0, 0])

    Starting with C++11, it’s possible to give non-static struct members a default value:

    从C ++ 11开始,可以为非静态struct成员提供默认值:

    struct Point { float x{0.0}; float y{0.0};};

    Initialize the namedtuple using a syntax similar to calling a constructor. Using a positional or keyword argument.

    使用类似于调用构造函数的语法初始化 namedtuple。 使用位置或关键字参数。

    ntpt = Point(3, y=6)

    Access the fields of a namedtuple similar to a structure

    访问类似于结构的namedtuple的字段

    ntpt.x + ntpt.y

    Or as the name suggests, as a tuple

    或顾名思义,如元组

    ntpt[0] + ntpt[1]

    Set a field using replace

    使用替换设置字段

    ntpt._replace(x=5)

    There are a few other things that you can do with a namedtuple which are python specific.

    您可以对namedtuple执行其他一些特定于python的操作。

    Such as initializing the fields using a list using p._make(list) and generate a dict for the fields and values using p._asdict().

    例如使用p._make(list)使用列表初始化字段,并使用p._asdict()为字段和值生成字典。

    You can list the field names using _fields and default values for fields using _fields_defaults.

    您可以使用列表中的字段名_fields和默认值使用字段_fields_defaults 。

    There is an alternative in python, a typed version of a namedtuple called NamedTuple under typing.

    在Python替代,namedtuple的类型化版本,称为NamedTuple下打字 。

    Where you can add field types which are listed under annotations.

    您可以在其中添加注释下列出的字段类型的位置。

    from collections import NamedTuplep = NamedTuple('Point', [('x', float), ('y', float)])

    To provide a default value you have to define a class that inherits NamedTuple.

    要提供默认值,您必须定义一个继承NamedTuple的类。

    class Point(NamedTuple): x: float = 0.0 y: float = 0.0

    Using typing.NamedTuple inherited class, defeats the purpose of defining a user defined custom data structure. You might as well just use a regular class.

    使用type.NamedTuple继承的类, 无法达到定义用户定义的自定义数据结构的目的。 您不妨只使用常规类。

    Hope this helps. Let me know if you have found any other alternatives.

    希望这可以帮助。 如果您找到其他替代方法,请告诉我。

    翻译自: https://medium.com/@arccoder/structures-in-python-ed199411b3e1

    相关资源:python 结构体 引用自身
    Processed: 0.010, SQL: 9