从Python 2.7更改为3.5无法正常工作

我有一个在Python 2.7上运行的程序,该程序在Python 3.5中创建了不同的输出,但我不明白为什么。请帮忙!

from ctypes import *         #ctypes laed die Libs in Python,array (BuildIn) fuer C Arrays
from array import *       #ctypes laed die Libs in Python,array (BuildIn) fuer C Arrays

class YasdiMaster:
    def __init__(self,ini_file="/home/pi/rs485/yasdi.ini",yasdiMaster_lib="libyasdimaster.so",iDeviceHandleCount=500,iChannelHandleCount=500,DeviceNameBuffer=30,DeviceTypeBuffer=30,ValText=15,ChannelName=30,cChanUnit=10,status_text_buffer=30):
        self.ini_file = ini_file
        self.yasdiMaster_lib = yasdiMaster_lib
        self.DriverCount = c_ulong()
        self.pDriverCount = pointer(self.DriverCount)
        self.iDeviceHandleCount = iDeviceHandleCount
        self.DeviceHandles = array("L",[0]*self.iDeviceHandleCount)
        self.iChannelHandleCount = iChannelHandleCount
        self.ChannelHandles = array("L",[0]*self.iChannelHandleCount)
        self.DeviceNameBuffer = " "*DeviceNameBuffer
        self.DeviceTypeBuffer = " "*DeviceTypeBuffer
        self.snBuffer = c_ulong()
        self.psnBuffer = pointer(self.snBuffer)
        self.dDevHandle = c_ulong()
        self.pdDevHandle = pointer(self.dDevHandle)
        self.ChannelName = " "*ChannelName
        self.dblValue = c_double(0)
        self.pdblValue = pointer(self.dblValue)
        self.ValText = " "*ValText
        self.cChanUnit = " "*cChanUnit
        self.status_text_buffer = " "*status_text_buffer
        self.ChanType = c_ushort()
        self.pChanType = pointer(self.ChanType)
        self.ChanIndex = c_int()
        self.pChanIndex = pointer(self.ChanIndex)
        self.range_min = c_double()
        self.prange_min = pointer(self.range_min)
        self.range_max = c_double()
        self.prange_max = pointer(self.range_max)

Master = YasdiMaster()
print(vars(Master)) 

这是Python 2.7的输出:

{'psnBuffer': <__main__.LP_c_ulong object at 0xb6a49580>,'range_min': c_double(0.0),'range_max': c_double(0.0),'cChanUnit': '          ','DeviceHandles': array('L',[0L,0L,0L]),'ValText': '               ','snBuffer': c_ulong(0L),'iDeviceHandleCount': 500,'ChanType': c_ushort(0),'dDevHandle': c_ulong(0L),'ChannelHandles': array('L','status_text_buffer': '                              ','pChanType': <__main__.LP_c_ushort object at 0xb6a49760>,'pdDevHandle': <__main__.LP_c_ulong object at 0xb6a49620>,'iChannelHandleCount': 500,'pChanIndex': <__main__.LP_c_long object at 0xb6a49800>,'ChannelName': '                              ','DeviceNameBuffer': '                              ','pdblValue': <__main__.LP_c_double object at 0xb6a496c0>,'pDriverCount': <__main__.LP_c_ulong object at 0xb6a494e0>,'ChanIndex': c_long(0),'yasdiMaster_lib': 'libyasdimaster.so','ini_file': '/home/pi/rs485/yasdi.ini','DeviceTypeBuffer': '                              ','dblValue': c_double(0.0),'DriverCount': c_ulong(0L),'prange_min': <__main__.LP_c_double object at 0xb6a498a0>,'prange_max': <__main__.LP_c_double object at 0xb6a49940>}

在Python 3.5中有一个不同的输出:

{'range_min': c_double(0.0),'pdblValue': <__main__.LP_c_double object at 0xb69d0170>,'prange_max': <__main__.LP_c_double object at 0xb69d03f0>,'pdDevHandle': <__main__.LP_c_ulong object at 0xb69d0120>,[0,0]),'snBuffer': c_ulong(0),'prange_min': <__main__.LP_c_double object at 0xb69d0350>,'pChanType': <__main__.LP_c_ushort object at 0xb69d0210>,'pChanIndex': <__main__.LP_c_long object at 0xb69d02b0>,'pDriverCount': <__main__.LP_c_ulong object at 0xb699e4e0>,'DriverCount': c_ulong(0),'psnBuffer': <__main__.LP_c_ulong object at 0xb699e8f0>,'dDevHandle': c_ulong(0),'DeviceNameBuffer': '                              '}

为什么创建的数组DeviceHandle和ChannelHandle不同? (0 vs. 0L)?

syldjzl88888 回答:从Python 2.7更改为3.5无法正常工作

根据Python文档,array类型代码L的Python类型在2.7.x中是long,而在3.5中是int。这应该可以解释为什么它在2.7.x输出中显示为0L而在3.5.x输出中显示为0

参考: -https://docs.python.org/3.5/library/array.html -https://docs.python.org/2.7/library/array.html

本文链接:https://www.f2er.com/2996957.html

大家都在问