Double underscore names

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Feb 12 17:55:39 EST 2008


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

> having the ability to create a protocol is a Very Good Thing, and
> double leading and trailing underscore names are the accepted Python
> style for such special methods.

Is it? There are many protocols that use plain names. Even the
built-in types support many "protocol" operations via plain-named
attributes.

    dict.get, dict.items, dict.keys, dict.values

    list.insert, list.append, list.extend, list.remove

    file.read, file.write, file.close

The double-underscore convention seems more for attributes *that are
interpreted specially*, e.g. by syntax operators or other core
language features.

Go ahead and implement your protocol using attributes with plain
names. What makes it a protocol is that it's likely to be generally
applicable, and the names are chosen to apply to other classes equally
well.

-- 
 \     “I cannot conceive that anybody will require multiplications at |
  `\        the rate of 40,000 or even 4,000 per hour …” —F. H. Wales, |
_o__)                                                             1936 |
Ben Finney



More information about the Python-list mailing list