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


Groups > comp.lang.python > #107746 > unrolled thread

Re: Pythonic style

Started byBen Finney <ben+python@benfinney.id.au>
First post2016-04-28 13:23 +1000
Last post2016-04-28 15:31 +1000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Pythonic style Ben Finney <ben+python@benfinney.id.au> - 2016-04-28 13:23 +1000
    Re: Pythonic style Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-04-28 15:31 +1000

#107746 — Re: Pythonic style

FromBen Finney <ben+python@benfinney.id.au>
Date2016-04-28 13:23 +1000
SubjectRe: Pythonic style
Message-ID<mailman.164.1461813821.32212.python-list@python.org>
Christopher Reimer <christopher_reimer@icloud.com> writes:

> In short,  my original code before I turned it into a separate
> dictionary. *sigh*

No, I think that misses the points that were being made. The discussion
you're talking about was *not* to say “attribute access is better than
dictionary access”, or vice versa. Each is good for its purpose.

Rather, the discussion was to drive home the point that in Python those
are *two distinct concepts*, and you need to not conflate them.

If you want items in a mapping, explicitly use a Python ‘dict’ instance.
If you want attributes that describe an object, explicitly use
attributes of that object. Deliberately choose which one makes more
sense.

-- 
 \         “The double standard that exempts religious activities from |
  `\       almost all standards of accountability should be dismantled |
_o__)                   once and for all.” —Daniel Dennett, 2010-01-12 |
Ben Finney

[toc] | [next] | [standalone]


#107761

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2016-04-28 15:31 +1000
Message-ID<5721a04d$0$2781$c3e8da3$76491128@news.astraweb.com>
In reply to#107746
On Thursday 28 April 2016 13:23, Ben Finney wrote:

> Christopher Reimer <christopher_reimer@icloud.com> writes:
> 
>> In short,  my original code before I turned it into a separate
>> dictionary. *sigh*
> 
> No, I think that misses the points that were being made. The discussion
> you're talking about was *not* to say “attribute access is better than
> dictionary access”, or vice versa. Each is good for its purpose.
> 
> Rather, the discussion was to drive home the point that in Python those
> are *two distinct concepts*, and you need to not conflate them.
> 
> If you want items in a mapping, explicitly use a Python ‘dict’ instance.
> If you want attributes that describe an object, explicitly use
> attributes of that object. Deliberately choose which one makes more
> sense.


Think of it like this: attribute access is for accessing parts of a thing:

dog.tail
car.engine
cup.handle

or one of a small number of fixed fields from a record:

employment_record.start_date
employee.salary
autopsy.cause_of_death
person.title


Mappings (dicts) are for recording a potentially unlimited number of 
distinct records with arbitrary keys, especially since the keys don't have 
to be valid identifiers:

employees['Fred Smith']
students['Joanna McTavish']
prisoners[12345]
catalog['Widgets']['GMH-12345']
dictionary['update']


While Python allows you to write code to use attribute syntax for item 
access, doing so is a category mistake and leads to semantic confusion and 
potential bugs:

employees.Fred_Smith
students.Joanna_McTavish
prisoners.P12345
parts.widgets.GMH_12345
dictionary.update


The last example shows how mixing up these two distinct concepts can lead to 
problems. Should dictionary.update refer to the update method, or the entry 
for the word "update"?



-- 
Steve

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web