2012-12-12 2 views
4
내가 PEP8과 유래에 대한 몇 가지 질문을 읽고 있었다, 그러나 의견 사이의 공간에 대해 궁금

:코드를 주석하는 적절한 방법. 파이썬

class MyBrowser(QWebPage): 
    ''' Settings for the browser.''' 

    def __init__(self): 
     QWebPage.__init__(self) 
     # Specifies whether images are automatically loaded in web pages. 
     self.settings().setAttribute(QWebSettings.AutoLoadImages, True) 

    def userAgentForUrl(self, url): 
     ''' Returns a User Agent that will be seen by the website. ''' 
     return "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15" 

사이에 빈 줄을 퍼팅의 가장 파이썬 방법입니다 무엇 :

는이 코드가 있다고 할 수 있습니다 주석과 실제 코드? 일부 전문가에게 내 프로그램을 보여주고 싶습니다. 내 코드가 더 전문적으로 보이기를 바랍니다.

+0

나는 현재 간격이 적당하다고 생각합니다. –

+0

전문가에 따르면, 나는 어떤 회사에서 매니저를 고용하는 것을 의미한다. – Vor

+0

그들은 전문가가 아닐 수도있다. .... 종종 고용주는 그들이 원하는 것을 당신을 몰래 기꺼이 ... 그들이보고 싶어하는 것은 당신이 파이썬 개념 교장 ... 그리고 당신은 컴파일 된 것과 해석 된 것의 차이점 중 일부를 알고 있다고 생각합니다. –

답변

9

그들이 관계로이 (이하 "지역 사회의 표준"을 나타냅니다 그러나 여기 Google's Python style guides 경우 나도 몰라 의견에). 구체적으로 클래스 :

class SampleClass(object): 
    """Summary of class here. 

    Longer class information.... 
    Longer class information.... 

    Attributes: 
     likes_spam: A boolean indicating if we like SPAM or not. 
     eggs: An integer count of the eggs we have laid. 
    """ 

    def __init__(self, likes_spam=False): 
     """Inits SampleClass with blah.""" 
     self.likes_spam = likes_spam 
     self.eggs = 0 

    def public_method(self): 
     """Performs operation blah.""" 
2

Zen of Python : "가독성이 중요합니다." 무엇이든간에 은 내가 가장 잘 읽을 수있는 것으로 나타났습니다.

+1

일반적으로, 때때로 파이썬 대중 (당신과는 반대로)이 더 읽기 쉽다고 생각합니다. 일반적으로 이것은 사실입니다.) –

+0

나는 그것이 무엇을하는지 이미 알고 있기 때문에 주석이 없으면 읽을 수없는 코드를 종종 찾습니다. 그러나 이것이 코멘트를 포함하면 안된다는 것을 의미하지는 않습니다. – djechlin

+0

나는 당신이 선호하는 것이 최선이라는 것에 동의하지 않는다. 저는 기묘한 표준 (예를 들어, 세 개의 들여 쓰기)을 따르는 사람들과 일했습니다. 그들은 그들의 코드를 좋아했습니다. 그룹의 아무도 그걸 보려고 설 수 없습니다. –

2

의심 스럽다면 표준 라이브러리에서 모델을 찾습니다. 여기

은 (귀도 반 로섬 (Guido van Rossum)이 직접 작성)을 timeit 모듈에서 발췌 한 것입니다 :

def print_exc(self, file=None): 
    """Helper to print a traceback from the timed code. 

    Typical use: 

     t = Timer(...)  # outside the try/except 
     try: 
      t.timeit(...) # or t.repeat(...) 
     except: 
      t.print_exc() 

    The advantage over the standard traceback is that source lines 
    in the compiled template will be displayed. 

    The optional file argument directs where the traceback is 
    sent; it defaults to sys.stderr. 
    """ 
    import linecache, traceback 
    if self.src is not None: 
     linecache.cache[dummy_src_name] = (len(self.src), 
              None, 
              self.src.split("\n"), 
              dummy_src_name) 
    # else the source is already stored somewhere else 

    traceback.print_exc(file=file) 
관련 문제