首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么double被认为是全局名称?

为什么double被认为是全局名称?
EN

Stack Overflow用户
提问于 2011-11-25 14:18:28
回答 4查看 6.3K关注 0票数 1

我已经创建了下面的代码,当我导入模块并尝试运行它时,我收到了以下错误:

代码语言:javascript
复制
>>> import aiyoo
>>> aiyoo.bixidist(1,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "aiyoo.py", line 50, in bixidist
    currentDist = dist(X,Y,c)
  File "aiyoo.py", line 39, in dist
    distance = math.sqrt(math.pow((X-getLat(i)),2)+math.pow((Y-getLong(i)),2))
  File "aiyoo.py", line 28, in getLat
    xmlLat = double(xmlLat)
NameError: global name 'double' is not defined

使用double函数是为了将XML的unicode输出转换为double作为后续函数的输入。所以我不明白为什么,当aiyoo模块被导入时,它被认为是一个名称。

下面是名为aiyoo.py的模块:

代码语言:javascript
复制
import math
import urllib2
from xml.dom.minidom import parseString
file = urllib2.urlopen('http://profil.bixi.ca/data/bikeStations.xml')
data = file.read()
file.close()
dom = parseString(data)

#this is how you get the data
def getID(i):
    xmlID = dom.getElementsByTagName('id')[i].toxml()
    xmlID = xmlID.replace('<id>','').replace('</id>','')
    xmlID = int(xmlID)
    return xmlID

def getLat(i):
    xmlLat = dom.getElementsByTagName('lat')[i].toxml()
    xmlLat = xmlLat.replace('<lat>','').replace('</lat>','')
    xmlLat = double(xmlLat)
    return xmlLat

def getLong(i):
    xmlLong = dom.getElementsByTagName('long')[i].toxml()
    xmlLong = xmlLong.replace('<long>','').replace('</long>','')    
    xmlLong = double(xmlLong)
    return xmlLong

#this is how we find the distance for a given station
def dist(X,Y,i):
    distance = math.sqrt(math.pow((X-getLat(i)),2)+math.pow((Y-getLong(i)),2))
    return distance

#this is how we find the closest station
def bixidist(X,Y):
     #counter for the lowest
    lowDist = 100000
    lowIndex = 0
    c = 0
    end = len(dom.getElementsByTagName('name'))
    for c in range(0,end):
            currentDist = dist(X,Y,c)
            if currentDist < lowDist:
                lowIndex = c
                lowDist = currentDist
    return getID(lowIndex)
EN

回答 4

Stack Overflow用户

发布于 2011-11-25 14:37:44

正如其他人回答的那样,double不是python中的内置类型。您必须使用,float。浮点是在C[ ref ]中使用双精度实现的。

至于你问题的主要部分,即“为什么双字词被认为是全局名称?”,当你使用一个variable-name,比如说double,这在本地上下文中找不到,下一次查找是在全局上下文中。然后,如果即使在全局上下文中也找不到它,就会引发异常,比如NameError: global name 'double' is not defined

祝你编码愉快。

票数 3
EN

Stack Overflow用户

发布于 2011-11-25 14:25:04

Python中没有double类型。如果您查看错误,它会报告找不到名为double的任何内容。Python中的浮点类型被命名为float

票数 1
EN

Stack Overflow用户

发布于 2011-11-25 14:23:39

应该是xmlLat = float(xmlLat)

Python float与其他语言的double相同。( 64位)

http://codepad.org/AayFYhEd

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8265410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档