2011-08-05 5 views
0

이 코드는 소스 클래스의 __str __method이 self._path파이썬 구글 폐쇄 컴파일러 소스 클래스 질문

은 자기를위한 특별한 속성입니다을 언급 http://code.google.com/p/closure-library/source/browse/trunk/closure/bin/build/source.py

복사입니까? 사촌, 난 장소를 찾을 수 없습니다

_path 단지 나 나가 다른 속성과 같은 개체에 설정되지 않을 수 속성입니다, 소스 클래스

import re 

_BASE_REGEX_STRING = '^\s*goog\.%s\(\s*[\'"](.+)[\'"]\s*\)' 
_PROVIDE_REGEX = re.compile(_BASE_REGEX_STRING % 'provide') 
_REQUIRES_REGEX = re.compile(_BASE_REGEX_STRING % 'require') 

# This line identifies base.js and should match the line in that file. 
_GOOG_BASE_LINE = (
    'var goog = goog || {}; // Identifies this file as the Closure base.') 


class Source(object): 
    """Scans a JavaScript source for its provided and required namespaces.""" 

    def __init__(self, source): 
    """Initialize a source. 

    Args: 
     source: str, The JavaScript source. 
    """ 

    self.provides = set() 
    self.requires = set() 

    self._source = source 
    self._ScanSource() 

    def __str__(self): 
    return 'Source %s' % self._path #!!!!!! what is self_path !!!! 

    def GetSource(self): 
    """Get the source as a string.""" 
    return self._source 

    def _ScanSource(self): 
    """Fill in provides and requires by scanning the source.""" 

    # TODO: Strip source comments first, as these might be in a comment 
    # block. RegExes can be borrowed from other projects. 
    source = self.GetSource() 

    source_lines = source.splitlines() 
    for line in source_lines: 
     match = _PROVIDE_REGEX.match(line) 
     if match: 
     self.provides.add(match.group(1)) 
     match = _REQUIRES_REGEX.match(line) 
     if match: 
     self.requires.add(match.group(1)) 

    # Closure's base file implicitly provides 'goog'. 
    for line in source_lines: 
     if line == _GOOG_BASE_LINE: 
     if len(self.provides) or len(self.requires): 
      raise Exception(
       'Base files should not provide or require namespaces.') 
     self.provides.add('goog') 


def GetFileContents(path): 
    """Get a file's contents as a string. 

    Args: 
    path: str, Path to file. 

    Returns: 
    str, Contents of file. 

    Raises: 
    IOError: An error occurred opening or reading the file. 

    """ 
    fileobj = open(path) 
    try: 
    return fileobj.read() 
    finally: 
    fileobj.close() 

답변

1

없음에서이 변수를 정의합니다. 선두의 밑줄은 단순히 저자가 그것이 객체의 내부 세부 사항이며 공용 인터페이스의 일부로 간주되기를 원하지 않는다고 느꼈다는 것을 의미합니다.

이 특별한 경우에, 무언가가 그 소스 파일 외부에서 속성을 설정하지 않으면, 그것은 단순히 실수 인 것처럼 보입니다. 누구도 Source 개체에 str()을 호출하려고 시도하지 않는 한 아무런 해가되지 않으며 아마도 아무도 시도하지 않습니다.

아직, 당신은 self에 대해 특별한 것이 있다고 생각하는 것 같습니다. self이라는 이름은 어떤 식 으로든 특별하지 않습니다.이 이름을 메서드의 첫 번째 매개 변수로 사용하는 것이 관습이지만 처리되는 개체를 참조하는 다른 이름과 같은 이름입니다. 따라서 오류가 발생하지 않고 self._path에 액세스 할 수 있다면 객체의 다른 이름을 통해 똑같이 액세스 할 수 있습니다.

관련 문제