[Tutor] Const on Python

Andreas Kostyrka andreas at kostyrka.org
Thu Mar 6 14:47:38 CET 2008

Well, as a philosophical argument, the public/private classification is
broken by design.


Assuming that one considers the "protection" part as useful (which
experience with dynamic languages like Python suggest is not axiomatic),
because it forces you to map all roles of a given class to a fixed
number of roles (public, protected, private in C++).

Consider for example a buffered data stream. Now I can envision the
following usage cases:

1.) just a simple "I want to write something" customer.
2.) "I need more control about the buffering" customer.
3.) "I want to implement the lowlevel IO" subclass.
4.) "I want to change/implement the buffering" subclass.

Now a good language (in the static language world) should allow me
specify this in a way, so that I can see for example what user classes
need updates if I change say the buffering implementation.

As an example for such an implementation, see the Modula 3 language. ;)

But that was only a visit into the reasoning that "static language"
advocates might find relevant. OTOH, the dynamic camp usually don't care
much for that kinds of access restrictions. (Well, Smalltalk does
disallow member variable access from outside of an object, BUT that's
just to force people to use methods. There is again no access control
system for methods.)


Am Donnerstag, den 06.03.2008, 10:08 -0300 schrieb Tiago Katcipis:
> 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> wrote:
>         The answer is slightly more complex.
>         1.) objects are either mutable or immutable. E.g. tuples and
>         strings are
>         per definition immutable and "constant". Lists and
>         dictionaries are an
>         example of the mutable kind.
>         2.) "variables", "instance members" are all only references to
>         objects.
>         Examples:
>         # lists are mutable
>         a = [1, 2]
>         b = a
>         b.append(3)
>         assert a == [1, 2, 3]
>         Now coming back to your question, that you want a
>         non-changeable name,
>         well, one can create such a beast, e.g.:
>         def constant(value):
>            def _get(*_dummy):
>                return value
>            return property(_get)
>         class Test(object):
>            const_a = constant(123)
>         This creates a member that can only be fetched, but not set or
>         deleted.
>         But in practice, I personally never have seen a need for
>         something like
>         this. You can always overcome the above constant. Btw, you can
>         do that
>         in C++ privates too, worst case by casting around and ending
>         up with a
>         pointer that points to the private element ;)
>         Andreas
>         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
>         > _______________________________________________
>         > Tutor maillist  -  Tutor at python.org
>         > http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20080306/da4665f8/attachment.pgp 

More information about the Tutor mailing list