Ctypes结构自动完成 [英] Ctypes Structure AutoComplete

查看:244
本文介绍了Ctypes结构自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python IDE(任何)Intellisense如何在设计时发现结构成员?

How is that possible for Python IDE(any) Intellisense to discover structure members in design time?

    class MY_STRUCTURE(ctypes.Structure):
        _fields_ = [("member1", c_int),
                    ("member2", c_int)]

         #Any idea declare the fields like self.member1 here inside the structure?

    my_structure = MY_STRUCTURE()
    my_structure.member1 = 0

这在运行时是可以的,但是在键入 my_structure时,智能感知有什么方法可以检测到my_structure内部有一个member1。

This is O.K in runtime but is there any way for intellisense to detect that there is a member1 inside the my_structure when typing "my_structure." ?

为此,我创建了一个由相同成员名称组成的python包装类,并在需要时对其进行均衡/绑定。但是我觉得这是一种肮脏的方式。

Just for this purpose I create a python wrapper class consist of same member names and equalize/bind them whenever required. However I feel it is a dirty way.

我将分发一个python SDK,如果将ctypes结构编译为字节码(没有源代码),那么将不会有任何想法如果没有提供额外的文档,则用户是结构的成员。这样会使开发人员和用户的工作更加复杂。

I will distribute a python SDK, if ctypes structure is compiled to bytecode(no source code) then there wont be any idea for the user what is the member of the structures if no extra documentation is provided. This will make both developer and user work more sophisticated.

推荐答案

我从@Alfe响应中了解到,这取决于IDE。 Python IDLE在设计时会找到结构的成员。 PyCharm不能。

I understand from @Alfe response that this depends on the IDE. Python IDLE find the members of the structure in design time. PyCharm cannot.

我通过以下方式使用我的结构,并且看起来工作正常。通过这种方式,也可以使用python类功能扩展ctypes.structure。

I use my structure with following way, and looks working fine. By this way ctypes.structure can also be expanded with python class features.

import ctypes
from ctypes import *

    class MY_STRUCTURE(ctypes.Structure):
        def __init__(self):
            self.member1 = 1
            self.member2 = 2
            super().__init__(member1=self.member1,
                         member2=self.member2)

        _fields_ = [("member1", c_int),
                    ("member2", c_int)]

此处的关键是调用super()基类。

The key here is to call super() base class.

这篇关于Ctypes结构自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆