python結(jié)構(gòu)體函數(shù) Python結(jié)構(gòu)體

python中定義的結(jié)構(gòu)體問題: 類似c語言中的如下這種形式 typedef struct { int x; int y; int h; }point;

class?block():

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)與策劃設(shè)計,鄱陽網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:鄱陽等地區(qū)。鄱陽做網(wǎng)站價格咨詢:028-86922220

def?__init__(self):

self.x=0

self.y=0

self.z=0

point=[block()?for?i?in?range(100)]

Python向怎么向C語言傳遞結(jié)構(gòu)體

況如下:

打算從Python發(fā)一個TCP數(shù)據(jù)包給遠(yuǎn)程服務(wù)器,數(shù)據(jù)的主體是一個C語言的 struct (較大,size 為1402)。由于這個struct太復(fù)雜,故不打算在python 處對其重新定義,目前的想法是用python調(diào)用一個C語言的模塊,在這個模塊中定義這個Struct,并設(shè)置好數(shù)據(jù)后,將其struct傳回python中,再打包傳送服務(wù)器。

但是不知道如何將這個struct 變量從C語言 傳入Python中。嘗試用Py_BuildValue函數(shù),以Py_BuildValue("P",interface_setup) //interface_setup為結(jié)構(gòu)體變量

傳遞,

但是幾次都得到運(yùn)行時錯誤:

SystemError: bad format char passed to PyBuildVaule。

python里面可以定義結(jié)構(gòu)體嗎

Python中沒有專門定義結(jié)構(gòu)體的方法,但可以使用class標(biāo)記定義類來代替結(jié)構(gòu)體,

其成員可以在構(gòu)造函數(shù)__init__中定義,具體方法如下。

復(fù)制代碼代碼如下:

class item:

def __init__(self):

self.name = '' # 名稱

self.size = 10 # 尺寸

self.list = [] # 列表

a = item() # 定義結(jié)構(gòu)對象

a.name = 'cup'

a.size = 8

a.list.append('water')

Python中如何使用C的結(jié)構(gòu)體struct求解

閟truct就可以使用結(jié)構(gòu)體了:

import struct

生成一個結(jié)構(gòu)體實例:

data = struct.pack( 'format_string', struct_menber_1, struct_menber_2, ... )

其中的format_string用來指定結(jié)構(gòu)體的格式(指明該結(jié)構(gòu)體在C中的定義),由兩部分組成:

首先是一個可選的特殊字符,用來指明字節(jié)序、數(shù)據(jù)類型大小和對齊方式:

@: native order, size alignment (default)

=: native order, std. size alignment

: little-endian, std. size alignment

: big-endian, std. size alignment

!: same as

然后是指明結(jié)構(gòu)體定義的部分:

The remaining chars indicate types of args and must match exactly;

these can be preceded by a decimal repeat count:

x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;

h:short; H:unsigned short; i:int; I:unsigned int;

l:long; L:unsigned long; f:float; d:double.

Special cases (preceding decimal count indicates length):

s:string (array of char); p: pascal string (with count byte).

Special case (only available in native format):

P:an integer type that is wide enough to hold a pointer.

Special case (not in native mode unless 'long long' in platform C):

q:long long; Q:unsigned long long

Whitespace between formats is ignored.

如果struct模塊的函數(shù)出錯,將產(chǎn)生struct.error異常。

python如何傳遞給c++一個結(jié)構(gòu)體指針?前提是swig封裝的C++函數(shù),請寫出代

在封裝的代碼間傳遞指針你要確保他們運(yùn)行在相同的地址空間里,還要保證指針指向的內(nèi)存的生存期是安全的,否則這種思路就是錯誤的。實現(xiàn)方法舉例如下:

1、定義了C

結(jié)構(gòu)體和函數(shù)如下

typedef

struct

NameAge

{

char

name[20];

int

age;

}NameAge

,

*NameAgePointer;

void

test(NameAgePointer

p)

//

接收結(jié)構(gòu)體指針

{

//

do

something

with

p...

}

2、python定義結(jié)構(gòu)體如下

#python中結(jié)構(gòu)體定義

class

PyStruct():

def

__init__(self,

name,

age):

self.name

=

name

self.age

=

age

fred

=

PyStruct("fred",

5)

3、假設(shè)把第1步里的test封裝成example模塊,python導(dǎo)入example(既然你都會swig了,這個過程就不啰嗦了)

import

example

example.test(pointer(fred))

以上是基本思路,因為搭建開發(fā)環(huán)境和過程比較繁雜,沒有驗證過,但是應(yīng)該沒有大問題

新聞名稱:python結(jié)構(gòu)體函數(shù) Python結(jié)構(gòu)體
網(wǎng)頁路徑:http://muchs.cn/article16/hjspgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站維護(hù)、關(guān)鍵詞優(yōu)化、App開發(fā)小程序開發(fā)、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)