TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Practical guidelines for beautiful Python code

36 pointsby alonswartzover 14 years ago

2 comments

denoover 14 years ago
I'm not sure about #2. Is it really that good idea to extend types? Or to be precise — to extend non-abstract classes?<p>It leads to unintuitive behavior:<p><pre><code> &#62;&#62;&#62; IntField(5, 'abc') is IntField(5, 'abc') False </code></pre> whereas<p><pre><code> &#62;&#62;&#62; int(5) is int(5) True </code></pre> also:<p><pre><code> &#62;&#62;&#62; IntField(5, 'abc') == 5 True </code></pre> not to mention:<p><pre><code> &#62;&#62;&#62; IntField(5, 'abcd') == IntField(5, 'xyz') True </code></pre> IMO this would be better, more flexible approach:<p><pre><code> class MyIntField(object): @property def val(self): return self._val @property def name(self): return self._name def __init__(self, val, name): self._val = val self._name = name def __eq__(self, other): return (self.val == other.val) and \ (self.name == other.name)</code></pre>
lincolnqover 14 years ago
#1, "Object Oriented structure should always map to well defined mental concepts in the problem domain," is a recipe for producing excessively complex code. You end up focusing on the parts themselves, instead of their interactions, which is actually the important part.
评论 #2088843 未加载