Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107741

Pythonic style (was: Differences between Class(Object) and Class(Dict) for dictionary usage?)

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Pythonic style (was: Differences between Class(Object) and Class(Dict) for dictionary usage?)
Date 2016-04-28 12:07 +1000
Message-ID <mailman.159.1461809287.32212.python-list@python.org> (permalink)
References <5720357B.4060009@icloud.com> <CALwzidn0SN5yax3bfmTPDTxBvfr+bnVNzeUe3dZYrS9ji2WCtw@mail.gmail.com> <572166FA.3020108@icloud.com> <85y47yjxj5.fsf_-_@benfinney.id.au>

Show all headers | View raw


Christopher Reimer <christopher_reimer@icloud.com> 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Pythonic style (was: Differences between Class(Object) and Class(Dict) for dictionary usage?) Ben Finney <ben+python@benfinney.id.au> - 2016-04-28 12:07 +1000

csiph-web