栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

对于python初学者,如何使用python定义联合(使用ctypes)?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

对于python初学者,如何使用python定义联合(使用ctypes)?

您的点赞和关注将是我持续更新的动力呢.v

有不懂的问题可以私聊我哦!

**问题引发:**如何使用python的工会?

我刚刚开始使用python,我想知道如何用python定义联合(使用ctypes)?希望我通过ctypes支持工会是对的。例如,以下c代码是如何在python中的

struct test{char something[10];
int status;};struct test2{char else[10];
int status;int alive;};
union tests{struct test a;struct test2 b;};
struct tester{char more_chars[20];
int magic;union tests f;};

Thx,如果其他人正在寻找相同的答案,则添加简单示例

from ctypes import *class POINT(Structure): 
_fields_ = [("x", c_int), ("y", c_int)
]class POINT_1(Structure): 
_fields_ = [("x", c_int), ("y", c_int), ("z",c_int)]
class POINT_UNIOn(Union): 
_fields_ = [("a", POINT), ("b", POINT_1)]
class TEST(Structure):
 _fields_ = [("magic", c_int),
 ("my_union", POINT_UNIOn)]
testing = TEST()testing.magic = 10;
testing.my_union.b.x=100testing.my_union
.b.y=200testing.my_union.b.z=300
解决方案

看一下ctypes教程:可以使用ctypes.Union类:

class test(ctypes.Structure): 
# ...class test2(ctypes.Structure): 
# ...class tests(ctypes.Union):
 _fields_ = [("a", test), ("b", test2)]

最后多说一句,小编是一名python开发工程师,这里有我自己整理了一套最新的python系统学习教程,包括从基础的python脚本到web开发、爬虫、数据分析、数据可视化、机器学习等。想要这些资料的可以关注小编,并在后台私信小编:“01”即可领取。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/487998.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号