当前位置:首页 » 编程语言 » 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)]

热点内容
centos使用python 发布:2024-05-18 23:39:48 浏览:866
幻影天龙脚本 发布:2024-05-18 23:38:17 浏览:711
编程的py 发布:2024-05-18 23:36:22 浏览:73
安卓系统怎么改序列号 发布:2024-05-18 23:28:16 浏览:782
c语言中实数 发布:2024-05-18 23:21:03 浏览:894
服务器搭建题目 发布:2024-05-18 23:01:29 浏览:27
下载武装突袭后怎么进服务器 发布:2024-05-18 22:56:17 浏览:825
c语言字符串大写变小写 发布:2024-05-18 22:56:16 浏览:438
重启删除的文件夹 发布:2024-05-18 22:34:11 浏览:638
视频软件源码 发布:2024-05-18 22:22:24 浏览:429