[Tutor] Const on Python

Ricardo Aráoz ricaraoz at gmail.com
Fri Mar 7 14:54:49 CET 2008

Tiago Katcipis wrote:
> thanks for the help Andreas, i dont really need that much a const so i 
> wont do anything like that to have a const like data. I am very used to 
> java and c++, thats why i always used acess modifier, but i think i can 
> live without it now i know that it dont exist in python :P.
> On Thu, Mar 6, 2008 at 6:39 AM, Andreas Kostyrka <andreas at kostyrka.org 
> <mailto:andreas at kostyrka.org>> wrote:
>     Am Mittwoch, den 05.03.2008, 21:07 -0300 schrieb Tiago Katcipis:
>      > Its a simple question but i have found some trouble to find a good
>      > answer to it, maybe i just dont searched enough but it wont cost
>      > anything to ask here, and it will not cost to much to answer :-).
>     I have
>      > started do develop on python and i really liked it, but im still
>      > learning. Im used to develop on c++ and java and i wanted to know if
>      > there is any way to create a final or const member, a member that
>     after
>      > assigned cant be reassigned. Thanks to anyone who tries to help
>     me and
>      > sorry to bother with a so silly question. i hope someday i can be
>     able
>      > to help :-)
>      >
>      > best regards
>      >
>      > katcipis

In adition to all this I'd like to quote PEP 8
     In addition, the following special forms using leading or trailing
     underscores are recognized (these can generally be combined with 
any case

     - _single_leading_underscore: weak "internal use" indicator.  E.g. 
"from M
       import *" does not import objects whose name starts with an 

     - single_trailing_underscore_: used by convention to avoid 
conflicts with
       Python keyword, e.g.

       Tkinter.Toplevel(master, class_='ClassName')

     - __double_leading_underscore: when naming a class attribute, 
invokes name
       mangling (inside class FooBar, __boo becomes _FooBar__boo; see 

     - __double_leading_and_trailing_underscore__: "magic" objects or
       attributes that live in user-controlled namespaces.  E.g. __init__,
       __import__ or __file__.  Never invent such names; only use them
       as documented.

As far as I can understand it if I want a class attribute that I'd 
rather not have touched (weak internal use indicator, I just extend it's 
use from modules to classes) I'd name it _attribute, if I want an 
attribute that I forbid to touch (strong internal use indicator, modify 
at your own risk) then I name it __attribute. That should be enough for 
any grown up use.

PEP 8 : http://www.python.org/dev/peps/pep-0008/

More information about the Tutor mailing list