在python3.3.2中访问函数中的类变量

2024-09-30 05:31:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,我知道关于这个问题已经有很多讨论了,但是我仍然有一个问题。我试图从一个类访问一个变量到同一个类函数,这是一个错误

“AttributeError:'int'对象没有属性'isOpen'”

代码为:

class serialCommunication():
    ser = 0               #class global variable
    def ser_port():
        .....
    def ser_Burdrate():
        BurdRate = ( "4800",
                     "9600",
                     "19200",
                     "38400",
                     "57600",
                     "115200"
                     )
        BR = int(input("Enter the Burd Rate\n0\t--\t4800\n1\t--\t9600\n2\t--\t19200\n3\t--\t38400\n4\t--\t57600\n5\t--\t115200\n\n"))
        return Portno , int(BurdRate[BR]), timeout
    def ser_open():
        port = serialCommunication.ser_Burdrate()
        serialCommunication.ser = serial.Serial(port[0], port[1], timeout=port[2])
        port = serialCommunication.ser.getPort()
        print (serialCommunication.ser , '\r\n')
        .....
    def ser_Write():

        if (serialCommunication.ser.isOpen()):
            print ('open: ', serialCommunication.ser.getPort())
        elif (serialCommunication.ser.closed()):
            serialCommunication.ser_open()

请给我同样的建议

提前谢谢

谢谢你的建议我改了

ser = serial.Serial()

这是一个错误

“TypeError:'bool'对象不可调用”

在if语句中bool对象可以正确执行。。?你知道吗


Tags: 对象brportdef错误timeoutserialopen
1条回答
网友
1楼 · 发布于 2024-09-30 05:31:05

您正试图对int类型(serialCommunication类中声明的ser=0)调用方法.isOpen(),因为错误表明对象没有此方法。你知道吗

我看不到类的其余部分,但是您确定不应该直接引用类的方法吗?此外,如果没有实例,则调用的方法必须是以@classmethod decorator为前缀的类方法。你知道吗

相关问题 更多 >

    热门问题