[Tutor] Const on Python

Tiago Katcipis katcipis at inf.ufsc.br
Thu Mar 6 14:08:27 CET 2008


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 --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080306/3893ae74/attachment.htm 


More information about the Tutor mailing list