python - Some advice on the style of different ways to limit line length to comply with PEP8 -
i updating code comply pep8 maximum line length.
i have 2 questions on people think better style. first init of class (or function definition matter). people split arguments of function definition if line long this:
def __init__(self, title, url, teaser, parsedate, updatedate, redis, id=none):
or considered better split thát part second line otherwise long this:
def __init__(self, title, url, teaser, parsedate, updatedate, redis, id=none):
and secondly: in long if statements, don't find splitting of conditions add mucht clarity of code this:
if self.redis.exists('article:%s' % self.url)\ , self.redis.hexists('article:%s' % self.url, 'id'): self.id = self.redis.hget('article:%s' % self.url, 'id') else: self.id = self.redis.incr('articleid')
if written below, line definatily long, think here clearer let value of self.id depend on condition. in previous example, less clear me condition ends , contents of if block starts.
if self.redis.exists('article:%s' % self.url) , self.redis.hexists('article:%s' % self.url, 'id'): self.id = self.redis.hget('article:%s' % self.url, 'id') else: self.id = self.redis.incr('articleid')
what python pep8 guidelines point of view considered better code, since guidelines says should not follow guideline:
"when applying guideline make code less readable, used reading code follows pep."
any advice highly appreciated.
for __init__
function, go somewhere in middle , try put parameters on each line, finding logical place split line if possible:
def __init__(self, title, url, teaser, parsedate, updatedate, redis, id=none):
you might consider if there way refactor code avoid many arguments single method, or use **kwargs
, accept intended parameters , explicitly reject extras.
for conditional, take advantage of implicit line breaking allowed inside parentheses (which how can split parameters function across multiple lines). technique shown in pep 8 itself.
if (self.redis.exists('article:%s' % self.url) , self.redis.hexists('article:%s' % self.url, 'id'): self.id = self.redis.hget('article:%s' % self.url, 'id') else: self.id = self.redis.incr('articled')
in case, since construct key 3 separate times, might factor out temporary variable first.
key = 'article:%s' % (self.url,) if (self.redis.exists(key) , self.redis.hexists(key, 'id')): self.id = self.redis.hget(key, 'id') else: self.id = self.redis.incr('articled')
Comments
Post a Comment