在python中创建一个struct

时间:2014-09-10 12:09:41

标签: python struct pascal

目前我正在将一个pascal程序移植到python,并且我有以下代码:

ScanList  = Record
                            name    : string;
                            I       : Integer;
                            Lower,
                            Upper   : Array [1..20] of real;
                            step    : Array [1..20] of real;
                            counts  : Array [1..20] of Integer;
                           Pol     : Array [1..20] of Integer;
                            AverageNo: Array[1..20] of Integer;
                            Average : Array [1..20] of AveMode;

                            selected: Array [1..20] of Boolean;
                            saved,
                            loaded,

                            altered : Boolean;

                        end;

我已经将大部分内容重写为python,因此现在看起来像这样:

ScanList  = Record
        name = "Hello"
        I = 0
        Lower = [0 for i in range(20)]
        Upper = [0 for i in range(20)]
        step  = [0 for i in range(20)]
        counts = [0 for i in range(20)]
        Pol   = [0 for i in range(20)]
        AverageNo = [0 for i in range(20)]
        Average  = [0 for i in range(20)]

        selected = [0 for i in range(20)]
        saved = True
        loaded = True

        altered = True
    end

但现在我的问题是,我希望将它作为结构(如旧的pascal代码中)。我已经看过这个问题了:C-like structures in Python,但是在这个问题上我没有数组,但在我的内容中我有。如何将上面的代码移植到结构中? 谢谢!

1 个答案:

答案 0 :(得分:-1)

与您发布的问题中的another answer类似

class Record:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

scanlist = Record(
        name = "Hello",
        I = 0,
        Lower = [0 for i in range(20)],
        ...,
        loaded = true,
        altered = true)

请注意与您的代码存在一些差异 - 在python中没有end;传递给“struct”的参数用逗号分隔;在python中使用大写的var名称并不习惯。