Python 错误 - int 对象没有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22066426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:09:53  来源:igfitidea点击:

Python Error - int object has no attribute

pythonpython-2.7

提问by user2659890

The below code gives me the error, other than changing the module that plays the (winsound) sound, this worked fine on Python2.6 on Windows. Not sure where I have gone wrong on this one. This is being run on a Linux box, previously on a Windows machine. The version on Windows was 2.6 and version on Linux is 2.7.3.

下面的代码给了我错误,除了更改播放(winsound)声音的模块之外,这在 Windows 上的 Python2.6 上运行良好。不知道我在这个问题上哪里出错了。这是在 Linux 机器上运行的,以前是在 Windows 机器上运行的。Windows 上的版本是 2.6,Linux 上的版本是 2.7.3。

Traceback (most recent call last): File "CallsWaiting.py", line 9, in first_time = time.time() AttributeError: 'int' object has no attribute 'time'

回溯(最近一次调用):文件“CallsWaiting.py”,第 9 行,在 first_time = time.time() AttributeError: 'int' object has no attribute 'time'

import _mysql
import sys
import time
import os
import pygame
pygame.init()

time = 3
first_time = time.time()
last_time = first_time

while True:
    pass
    new_time = time.time()
    if new_time - last_time > timeout:
        last_time = new_time
        os.system('cls')
        iswaiting = 0

        print "Calls Waiting: "

        con = _mysql.connect(host='oip-prod', port=3308, user='admin', passwd='1234', db='axpdb')

        con.query("select callswaiting from callcenterinformation where date - date(now()) and skillid = 2 order by time desc limit 1;")
        result = con.user_result()
        iswaiting = int('',join(result.fetch_row() [0]))

        print "%s" % \
            iswaiting

        if iswaiting > 0:
            print "Calls are waiting!"
            pygame.mixer.init()
            sounda = pygame.mixer,Sound("ring2.wav")
            sounda.play()

采纳答案by DonCallisto

As time = 3is declared as an integer, time.timedoesn't have any sense since time is intvariable (that isn't a class but a primitive data type). I suppose that you expected to call time(module) writing timebut, since you're redefining it as an integer, this last definition shadows the timemodule

Astime = 3声明为整数, time.time没有任何意义,因为时间是int可变的(这不是类,而是原始数据类型)。我想您希望调用time(module) 编写,time但是,由于您将其重新定义为整数,因此最后一个定义会影响time模块

Change timevariable name to something else, like myTime

time变量名称更改为其他名称,例如myTime

Error messages are usefull, you should read them. Often the answer is contained directly into this errors/warning messages

错误消息很有用,您应该阅读它们。通常答案直接包含在此错误/警告消息中

回答by Andrey Rusanov

You have variable time:

你有可变的时间:

time = 3 

and you have previously imported package time:

并且您之前已导入包时间:

import time

When you try to do

当你尝试做

time.time() 

it seems that you try to call method time() of variable time (that contains int).

似乎您尝试调用可变时间(包含 int)的方法 time()。

You should rename it and it will figure out conflicts with package name.

您应该重命名它,它会找出与包名称的冲突。

回答by Nilesh

You declare timevariable. While timeis module imported from importstatement. So when you access time.xits try to access variable instead of module.

你声明time变量。whiletime是从import语句导入的模块。因此,当您访问time.x它时,请尝试访问变量而不是module.

Change variable name or import module timeas another name.

更改变量名称或导入模块time为另一个名称。