Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Pythonic style (was: Differences between Class(Object) and Class(Dict) for dictionary usage?) Date: Thu, 28 Apr 2016 12:07:58 +1000 Lines: 33 Message-ID: References: <5720357B.4060009@icloud.com> <572166FA.3020108@icloud.com> <85y47yjxj5.fsf_-_@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de l5xbLHXXIh7YA0D/F2dbGQODIUz3V3YhisiKlF3T2XUQ== Cancel-Lock: sha1:UYd/JDhGIjVntWVd7ulkVX3vqF4= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assignment': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Object': 0.09; 'python': 0.10; 'attributes,': 0.16; 'conveys': 0.16; 'magic': 0.16; 'python;': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'subject:between': 0.16; 'subject:usage': 0.16; 'syntactic': 0.16; 'terms).': 0.16; 'wrote:': 0.16; 'latter': 0.22; 'am,': 0.23; 'examples': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'invoke': 0.29; "people's": 0.29; 'subject:) ': 0.32; 'behind': 0.35; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'more': 0.63; '8bit%:21': 0.70; 'skip:\xe2 10': 0.70; '8bit%:43': 0.72; "(don't": 0.84; '_o__)': 0.84; 'expresses': 0.84; 'received:125': 0.84; 'subject:Class': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <85y47yjxj5.fsf_-_@benfinney.id.au> X-Mailman-Original-References: <5720357B.4060009@icloud.com> <572166FA.3020108@icloud.com> Xref: csiph.com comp.lang.python:107741 Christopher Reimer writes: > On 4/27/2016 7:33 AM, Ian Kelly wrote: > > > self.__dict__ = {'key', 'value'} > > > > self.key = value > > Which expression is Pythonic? (Note that assignment is not an expression in Python; assigment is a statement.) > I've seen both used in various examples on the Internet. I would say the latter is more Pythonic, because it: * Better conveys the intention (“set the value of the ‘self.key’ attribute”). * Uses the built-in mechanisms of Python (don't invoke magic attributes, instead use the system that makes use of them behind the scenes). * Expresses that intention more concisely (fewer terms). * Expresses that intention more clearly (less syntactic noise). -- \ “Nothing so needs reforming as other people's habits.” —Mark | `\ Twain, _Pudd'n'head Wilson_ | _o__) | Ben Finney