2014-09-10 51 views
-2

目前我移植一個Pascal程序蟒蛇,在那裏我有以下代碼:在Python中創建一個struct

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; 

我已經重寫了大部分進入蟒蛇,因此現在看起來是這樣的:

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,但在這個問題中,我沒有陣列,但在我的我。我如何將上面的代碼移植到結構中?
謝謝!

+0

閱讀這個你可以https://docs.python.org/2/library/struct.html#format-characters – 2014-09-10 12:12:27

+3

我真的不明白你的重寫代碼應該是Python。 – Marius 2014-09-10 12:15:39

+0

@Marius:問題在哪裏?我的python已經有點生疏了,正如我已經說過的那樣,它並沒有完全移植... – 2014-09-10 12:18:00

回答

-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名稱並不習慣。