當前位置:首頁 » 編程語言 » python結構體定義

python結構體定義

發布時間: 2023-05-21 12:26:17

A. 關於python ctypes里 Union的問題

DataValue(Structure):
_anonymous_ = ('DataValue',)
可能是寫錯了,改成:
class DataValue(Structure):
_anonymous_ = ('DataValue',)

data_value.DataValue = 23
這句報錯嗎?
換成:
data_value.i4Val = 32
試試

如果有問題,希望你把錯誤的原因(traceback)貼出來

B. python中定義的結構體問題: 類似c語言中的如下這種形式 typedef struct { int x; int y; int h; }point;

classblock():
def__init__(self):
self.x=0
self.y=0
self.z=0
point=[block()foriinrange(100)]

C. python 怎麼理解 類 和 self 的用法 和含義

首先你得先理解什麼是對象。假設生物是一個類,生物有生命,這個時候我就能在生物這個類里定義宴汪self.life=true。然後人類是生物的一種,那人類就可以繼承衫裂生物這個類,同時人類還有思想那我可以在人類這個類里定義self.think=true。之後晌塌仔我就可以實例化一個人和一個生物,這時被實例的人擁有思想和生命,而生物只有生命。

D. python如何傳遞給c++一個結構體指針前提是swig封裝的C++函數,請寫出代

在封裝的代碼埋讓間傳遞指針你要確保他們運行在相同的地址空間里,還要保證指針指向的內存的生存期是安全的,否則這種思路就是錯誤的。實現方法舉例如下:
1、定義了C
結構體和函數如下
typedef
struct
NameAge
{
char
name[20];
int
age;
}NameAge
,
*NameAgePointer;
void
test(NameAgePointer
p)
//
接收結構體指針
{
//
do
something
with
p...
}
2、滑陸python定義結構體如下
#python中結構體定義
class
PyStruct():
def
__init__(self,
name,
age):
self.name
=
name
self.age
=
age
fred
=
PyStruct("fred",
5)
3、假設把第1步里的test封裝成example模塊,python導入example(既然你都會swig了,這個過程就不啰嗦了)
>>>import
example
>>>example.test(pointer(fred))
以上是基本思路,因為搭建開發環境和過程比較彎讓局繁雜,沒有驗證過,但是應該沒有大問題

E. python怎麼導入ctypes

1. 載入Windows系統自帶的dll文件:
#載入cdecl調用約定的dll
msvcrt =cdll.msvcrt
#載入stdcall調用約定的dll
kernel32 =windll.kernel32
2. 載入自己dll文件,假如為addFuncDll,方式如下:
mydll =CDLL("addFuncDll.dll")
或者 mydll = cdll.addFuncDll
如果其中有函數add,計算兩個整數的和,則使用方式如下:
result=mydll.add(4,5)
可以多一步指明add函數的參數類型(也可不指明):
mydll.add.argtypes= [c_int,c_int]
3. 結構體在python中定義為Structure的子類如下:
class POINT(Structure):
_fields_ = [("x", c_int),
("y",c_int)]
_fields中每一項為元組(成員名稱,類型)
結構體還可以用於其他的結構體:
class RECT(Structure):
_fields_ = [("upperleft",POINT),
("lowerright",POINT)]

F. python裡面可以定義結構體嗎

Python中沒有專門定義結構體的方法,但可以使用class標記定義類來代替結構體,
其成員可以在構造函數__init__中定義,具體方法如下。
復制代碼代碼如下:

class item:
def __init__(self):
self.name = '' # 名稱
self.size = 10 # 尺寸
self.list = [] # 列表
a = item() # 定義結構對象
a.name = 'cup'
a.size = 8
a.list.append('water')

G. python定義模型

學python的人都知道,python中一切皆是對象,如class生成的對象是對象,class本身也是對象,int是對象,str是對象,dict是對象...。所以,我很好奇,python是怎樣實現這些對象的?帶著這份好奇,我決定去看看python的源碼,畢竟源碼才是滿足自己好奇心最直接的方法。

在object.h文件中,定義了兩種數據結構PyObject和PyVarObject,代碼如下:

1 #define PyObject_HEAD 2 Py_ssize_t ob_refcnt; 3 struct _typeobject *ob_type; 4 5 #define PyObject_VAR_HEAD 6 PyObject_HEAD 7 Py_ssize_t ob_size; 8 9 typedef struct _object {10 PyObject_HEAD11 } PyObject;12 13 typedef struct {14 PyObject_VAR_HEAD15 } PyVarObject;

這兩種數據結構分別對應python的兩種對象:固定長度對象和可變長度對象。python中的所有對象都屬於這兩種對象中的一種,如int,float是固定長度對象,list,str,dict是可變長度對象。從上面兩種對象數據結構定義來看,可變長度對象和固定長度對象的頭都是PyObject結構體,也就是說python中所有對象的開頭都包含這個結構體,並且可以用PyObject *指針來訪問任何對象,這種訪問對象的方法在python的源碼中隨處可見。PyObject結構體包含兩個成員,ob_refcnt和ob_type指針。ob_refcnt用來表示對象被引用的次數,當ob_refcnt == 0時,這個對象會被立即銷毀;ob_type指針指向了一個_typeobject類型的結構體,表示對象所屬的類型,也就是生成該對象的類型,這其實很類似於面向對象中類與實例的關系,PyObject是某個類的實例,ob_type表示這個類。但與面向對象不同的是,ob_type本身也是個對象,我們來看下_typeobject的定義:

1 typedef struct _typeobject { 2 PyObject_VAR_HEAD 3 const char *tp_name; /*類型名 */ 4 Py_ssize_t tp_basicsize, tp_itemsize; /* 實例化對象的大小 */ 5 6 /* 標准方法 */ 7 8 destructor tp_dealloc; 9 printfunc tp_print;10 getattrfunc tp_getattr;11 setattrfunc tp_setattr;12 cmpfunc tp_compare;13 reprfunc tp_repr;14 15 /* 標准類(數值類,列表類,dict類)方法*/16 17 PyNumberMethods *tp_as_number;18 PySequenceMethods *tp_as_sequence;19 PyMappingMethods *tp_as_mapping;20 21 /* 其它標准方法*/22 23 hashfunc tp_hash;24 ternaryfunc tp_call;25 reprfunc tp_str;26 getattrofunc tp_getattro;27 setattrofunc tp_setattro;28 ...
29 } PyTypeObject;

從上面定義來看,_typeobject的開頭也包含了PyObject結構體,所以它也是一個對象,既然它也是一個對象,那麼按照面向對象的理解,它又是誰來生成的呢?答案是所有PyTypeObject對象都是通過PyType_Type來生成的,包括PyType_Type本身,因為PyType_Type也是PyTypeObject對象,有點繞。PyType_Type的定義是通過將PyType_Type聲明為全局靜態變數實現的,具體如下:

1 PyTypeObject PyType_Type = { 2 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3 "type", /* tp_name */ 4 sizeof(PyHeapTypeObject), /* tp_basicsize */ 5 sizeof(PyMemberDef), /* tp_itemsize */ 6 (destructor)type_dealloc, /* tp_dealloc */ 7 0, /* tp_print */ 8 0, /* tp_getattr */ 9 0, /* tp_setattr */10 0, /* tp_compare */11 (reprfunc)type_repr, /* tp_repr */12 0, /* tp_as_number */13 0, /* tp_as_sequence */14 0, /* tp_as_mapping */15 (hashfunc)_Py_HashPointer, /* tp_hash */16 (ternaryfunc)type_call, /* tp_call */17 0, /* tp_str */18 (getattrofunc)type_getattro, /* tp_getattro */19 (setattrofunc)type_setattro, /* tp_setattro */20 0, /* tp_as_buffer */21 ...22 }

從PyType_Type定義來看,ob_type被初始化為它自己的地址,所以PyType_Type的類型就是自己。從python源碼實現來看,所有PyTypeObject的ob_type都會指向PyType_Type對象,所以PyType_Type是所有類型的類型,稱之為元類。python中定義了很多內建的類型對象,如PyInt_Type (int類型),PyStr_Type (str類型),PyDict_Type(dict類型) 類型對象,下面看下PyInt_Type類型的定義:

1 PyTypeObject PyInt_Type = { 2 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3 "int", 4 sizeof(PyIntObject), 5 0, 6 (destructor)int_dealloc, /* tp_dealloc */ 7 (printfunc)int_print, /* tp_print */ 8 0, /* tp_getattr */ 9 0, /* tp_setattr */10 (cmpfunc)int_compare, /* tp_compare */11 (reprfunc)int_to_decimal_string, /* tp_repr */12 &int_as_number, /* tp_as_number */13 0, /* tp_as_sequence */14 0, /* tp_as_mapping */15 (hashfunc)int_hash, /* tp_hash */16 0, /* tp_call */17 ...18 };

從PyInt_Type定義來看,它主要包含了int數據類型相關的方法。PyInt_Type類型對象的初始化和PyType_Type類型類似,PyInt_Type類型的定義也是通過全局靜態變數的方式實現的,除了PyInt_Type了下,所有python內建類型都是以這種方式定義的。這些類型產生的對象都會共享這些類型對象,包括這些類型定義的方法。

在python中,怎樣查看對象的類型呢?有兩種方法,一種是直接type:

1 >>> x = 12 >>> type(x)3 <type 'int'>

另一種是通過對象的__class__屬性:

1 >>> x = 12 >>> type(x)3 <type 'int'>4 >>> x.__class__5 <type 'int'>

現在來看看int,str,dict這些類型的類型:1 <type 'int'>2 >>> type(int)3 <type 'type'>4 >>> type(str)5 <type 'type'>6 >>> type(dict)7 <type 'type'>8 >>> type(type)9 <type 'type'>從這個輸出來看,int,str,dict這些類型的類型都是type,這也印證了前面說的,所有類型都是通過元類type生成的。

H. Python中如何使用C的結構體struct求解

閟truct就可以使用結構體了:
import struct
生成一個結構體實例:
data = struct.pack( 'format_string', struct_menber_1, struct_menber_2, ... )
其中的format_string用來指定結構體的格式(指明該結構體在C中的定義),由兩部分組成:
首先是一個可選的特殊字元,用來指明位元組序、數據類型大小和對齊方式:
@: native order, size & alignment (default)
=: native order, std. size & alignment
<: little-endian, std. size & alignment
>: big-endian, std. size & alignment
!: same as >
然後是指明結構體定義的部分:
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模塊的函數出錯,將產生struct.error異常。

I. python 列表的元素可以是結構體嗎

python里邊沒有結構體這個概念吧,這是c語言裡面的東東,不過,python里邊字典和結構體本質上是差不多的,你看看能把字典作為鏈表元素不,如果可以問題就解決了!

J. python如何定義二維結構體數組

Data[2,2]=[(2,3),(2,1)]

熱點內容
引流群控腳本 發布:2024-05-05 08:42:14 瀏覽:176
sql測試題和答案 發布:2024-05-05 08:42:09 瀏覽:892
https加密鏈接 發布:2024-05-05 08:28:35 瀏覽:444
對等區域網與客戶機伺服器有什麼不同 發布:2024-05-05 07:51:15 瀏覽:175
win7Linux修復linux 發布:2024-05-05 07:47:17 瀏覽:61
oracle批處理腳本 發布:2024-05-05 07:32:20 瀏覽:393
linuxftp響應慢 發布:2024-05-05 07:23:03 瀏覽:803
sql查詢所有欄位 發布:2024-05-05 07:22:07 瀏覽:672
電腦的存儲符號 發布:2024-05-05 07:15:21 瀏覽:132
sql轉換成數據類型int時失敗 發布:2024-05-05 06:29:21 瀏覽:827