2014-09-07 3 views
1

에 업데이트되지 나는 글로벌 VAR와 파이썬 파일을 가지고 클래스를 정의 :파이썬 글로벌 새 개체 인스턴스

ROOT = 'https://api.example.com/2.0/' 

class Mail(object): 
    def __init__(self, api_key): 
     self.name = "FName" 

     if api_key.find('-'): 
      dc = api_key.split('-')[1] 

     global ROOT 
     print "initing root to: %s" % (ROOT,) 
     ROOT = ROOT.replace('https://api.', 'https://'+dc+'.api.') 
     print "initing after replace of ROOT: %s" % (ROOT,) 

    def pprint(self): 
     print 'POST to %s' % (ROOT,) 

이 설정되어 일단 ROOT 세계가 갱신되지 않습니다 보인다

>>> import pyglob 
>>> mc = pyglob.Mail('dfadsfad-us1') 
initing root to: https://api.example.com/2.0/ 
initing after replace of ROOT: https://us1.api.example.com/2.0/ 
>>> mc.pprint() 
POST to https://us1.api.example.com/2.0/ 
>>> mc3 = pyglob.Mail('dfadsfad-us3') 
initing root to: https://us1.api.example.com/2.0/ 
initing after replace of ROOT: https://us1.api.example.com/2.0/ 
>>> mc3.pprint() 
POST to https://us1.api.example.com/2.0/ 
>>> mc.pprint() 
POST to https://us1.api.example.com/2.0/ 

누군가가 어떻게 작동하는지 설명하고 원인을 설명 할 수 있습니까? 'https://api.' 더 이상 대체 --you've ROOT에 있기 때문에 당신이 ROOT = https://us1.api.example.com/2.0/을 변경 한 후

+3

첫 번째 바꾸기 호출 이후에 대체 할 'https : // api.'가 없기 때문에. –

+0

@AshwiniChaudhary, 아! 그게 오는 것을 보지 못했습니다. – Gezim

답변

1

, ROOT.replace('https://api.', 'https://'+dc+'.api.')는 아무것도하지 않습니다.

이 전체적인 상황은 상당히 혼란스러워 보입니다. 아마도 전역 변수를 전혀 리 바인딩하지 않기를 원할 것입니다.

+0

쿨. 그러나 'replace'가 수정 되더라도 전역 변수를 공유하면 클래스의 인스턴스가 두 개 이상있을 때 충돌이 발생할 수 있다고 말하는 것이 안전합니다. – Gezim

+0

예. 인스턴스에 대해 뭔가를 원한다면, 그것을 'self'의 속성으로 만드십시오. –