
正文
python函数结构体 python 结构体
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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')
相关问答
Q1: Python 如何给 c 函数传递结构体参数
//test1.c# include stdio.h# include stdlib.hstruct Student
{ char name[30]; float fScore[3];
};void Display(struct Student su){ printf("-----Information------\n"); printf("Name:%s",su.name); printf("Chinese:%.2f\n",su.fScore[0]); printf("Math:%.2f\n",su.fScore[1]); printf("English:%.2f",su.fScore[2]); printf("平均分数为:%.2f\n",(su.fScore[0]+su.fScore[1],su.fScore[2])/3);
}
Q2: 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异常。
Q3: python如何传递给c++一个结构体指针?前提是swig封装的C++函数,请写出代
在封装python函数结构体的代码间传递指针你要确保python函数结构体他们运行在相同的地址空间里python函数结构体,还要保证指针指向的内存的生存期是安全的python函数结构体,否则这种思路就是错误的。实现方法举例如下:
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函数结构体,python导入example(既然你都会swig了,这个过程就不啰嗦了)
import
example
example.test(pointer(fred))
以上是基本思路,因为搭建开发环境和过程比较繁杂,没有验证过,但是应该没有大问题
Q4: python调用c语言动态库dll/.so中的函数的参数是结构体的问题
java源自C++,C++源自C语言....
各有优点呀,不知道要怎么回答了,或许楼主是搞C语言python函数结构体的吧,这些语言都各有特点呀...
首先应该清晰,Java是由C++发展而来的,python函数结构体他保留了c++的大部分内容,类似于c++,
但句法更清晰,规模更小,更易学。python函数结构体他是在对多种程式设计语言进行了深入细致研究的
基础上,据弃了其他语言的不足之处,从根本上解决了c++的固有缺陷,而产生的一种
新的完全方面向对象的语言。
Java和c++的相似之处多于不同之处,但两种语言问几处主要的不同使得Java更容易
学习,并且编程环境更为简单。
因篇幅所限,这里不能完全列出不同之处,仅列出比较显著的差别python函数结构体:
1.指针
Java无指针,并且增添了自动的内存管理功能,从而有效地防
止了c/c++语言中指针操作失误,如指针悬空所造成的系统崩溃。
比w操作返回一对象的引用,类似于c++中的引用python函数结构体;在c++中,
new返回一个对象的指针。在Java中无指针,不会遇见下面这样的
语句:
Mywork?>Mywork();
没有指针的程式无法访问不属于他的内存,消除了在c++
中?些常见的错误,这有利于Java程式的安全。
2.多重继承
c++支持多重继承,这是c++的一个特征,他允许多父类派
生一个类。尽管多重继承功能非常强,但使用复杂,而且会引起许多麻
烦,编译程式实现他也非常不容易。Java不支持多重继承,但允许一个
类继承多个接口(界面),实现了c++多重继承的功能,又避免了c++的
许多缺陷。
python函数结构体的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python 结构体、python函数结构体的信息别忘了在本站进行查找喔。






