2011-11-25 2 views
1
나는 아래의 코드를 생성 한

, 나는 모듈을 가져오고 그것이 내가 다음과 같은 오류 받아 실행하려고 할 때 : 이중 기능이로 XML에서 유니 코드 출력을 변환하기 위해 사용되었다왜 이중 명이 글로벌 이름으로 간주됩니까?

>>> 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 

을 따를 함수의 입력 값으로 두 배. 그래서 이유를 모르겠다. aiyoo 모듈을 가져올 때 이름으로 간주된다.

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) 

답변

1

파이썬에는 double 유형이 없습니다. 그리고 오류를 보면 double이라는 것을 찾을 수 없다는 불평을합니다. 파이썬의 부동 소수점 유형은 float입니다.

3

다른 사람들의 답변에 따르면 double은 python의 기본 제공 유형이 아닙니다. 대신 float을 사용해야합니다. 부동 소수점은 C [ref]에서 double을 사용하여 구현됩니다.

질문의 주요 부분 즉, "이중이 글로벌 이름으로 간주되는 이유는 무엇입니까?"라고 말하면 variable-name라고 말하면 double이라고 말하면 로컬 컨텍스트에는 없습니다. 다음 조회는 전역 컨텍스트입니다. 글로벌 컨텍스트에서도 발견되지 않으면 예외가 제기되어 NameError: global name 'double' is not defined이라고합니다.

해피 코딩.

0

다른 2 개의 대답과 마찬가지로 파이썬에는 double 변수 유형이없고 float이 있습니다.

제목에 질문이 있거나 혼란의 원인이 될 수 있습니다. 인터프리터가 "NameError : global name 'double'is not defined"라는 이유는 파이썬이 함수, 변수, 객체 등의 이름을 검색하는 방식 때문입니다.이 패턴은 파이썬의 네임 스페이스와 범위 규칙에 의해 설명됩니다. 존재하지 않는 함수 Double을 한정하지 않고 함수 (예 : SomeObject.Double(x))에서 호출하려고했기 때문에 Python은 먼저 로컬 네임 스페이스 (현재 실행중인 함수의 네임 스페이스)에서 해당 이름의 개체를 찾은 다음 로컬 네임 스페이스 그 다음에 글로벌 네임 스페이스, 그리고 마지막으로 네임 스페이스가 내장되어있다. 해석기가 당신에게 메시지를 보낸 이유는 파이썬이 Double()의 정의를 검색하는 순서 때문입니다. 전역 네임 스페이스는 빌트인 (파이썬의 코드가 아닌 코딩)에서 찾기 전에 체크 한 마지막 장소 였기 때문에 인터프리터가 "NameError : built-in name 'double'이라고 말하면 이해할 수없는 것 같아요. 정의되지 않음 "). 적어도 나는 이것이 계속되고 있다고 생각한다. 나는 여전히 숙련 된 프로그래머가 아니기 때문에, 뭔가 잘못 됐으면 다른 누군가가 나를 교정 할 것이라고 믿는다.

관련 문제