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


Groups > comp.lang.python > #20070

Re: Read-only attribute in module

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: Read-only attribute in module
References <mailman.5583.1328787848.27778.python-list@python.org>
Date 2012-02-09 23:32 +1100
Message-ID <878vkc426c.fsf@benfinney.id.au> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


Mateusz Loskot <mateusz@loskot.net> writes:

> I'm wondering, is there any mean to implement attribute in module
> scope which is read-only?

Python is designed by and for consenting adults. Rather than
restricting, instead use conventions to make your intent clear to the
user of your library.

> So, the following
>
> import xyz
> print(xyz.flag)  # OK
> xyz.flag = 0    # error due to no write access

PEP 8 <URL:http://www.python.org/dev/peps/pep-0008/> gives the style
guide for Python code (strictly for the standard library, but it is
recommended for all Python code).

If you want a module attribute that is intended to remain bound to the
same value, use PEP 8's recommendation and name the attribute in
‘ALL_UPPER_CASE’.

If you want an attribute that is intended only for internal use (an
implementation detail that should not be relied upon outside the
library), use PEP 8's recommendation and name the attribute with a
‘_single_leading_underscore’.

-- 
 \       “We jealously reserve the right to be mistaken in our view of |
  `\      what exists, given that theories often change under pressure |
_o__)              from further investigation.” —Thomas W. Clark, 2009 |
Ben Finney

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


Thread

Read-only attribute in module Mateusz Loskot <mateusz@loskot.net> - 2012-02-09 11:43 +0000
  Re: Read-only attribute in module Ben Finney <ben+python@benfinney.id.au> - 2012-02-09 23:32 +1100
    Re: Read-only attribute in module mloskot <mateusz@loskot.net> - 2012-02-09 08:44 -0800
    Re: Read-only attribute in module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-10 01:04 +0000
      Re: Read-only attribute in module Terry Reedy <tjreedy@udel.edu> - 2012-02-09 22:27 -0500
        Re: Read-only attribute in module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-10 22:23 +0000
      Re: Read-only attribute in module Arnaud Delobelle <arnodel@gmail.com> - 2012-02-10 10:09 +0000

csiph-web