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

Ben Finney ben+python at benfinney.id.au
Wed Apr 27 22:07:58 EDT 2016


Christopher Reimer <christopher_reimer at 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




More information about the Python-list mailing list