歡迎各位小哥哥小姐姐閱讀本<小生>的文章,對大家學習有幫助,請點贊加關注哦!!!!!!!!!!
您的點贊和關注將是我持續(xù)更新的動力呢.v
有不懂的問題可以私聊我哦!
問題引發(fā):如何使用python的工會?
我剛剛開始使用python,我想知道如何用python定義聯(lián)合(使用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=100
testing.my_union.b.y=200
testing.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開發(fā)工程師,這里有我自己整理了一套最新的python系統(tǒng)學習教程,包括從基礎的python腳本到web開發(fā)、爬蟲、數(shù)據(jù)分析、數(shù)據(jù)可視化、機器學習等。感興趣的+Q群:895817687