Defining a method final

Richard Barrett R.Barrett at ftel.co.uk
Tue Jun 11 19:09:53 CEST 2002


At 15:35 11/06/2002 +0000, Eric Brunel wrote:
>Emile van Sebille wrote:
> > Accepted practice is to use a single underscore for objects to be
> > considered private.  This does not prevent their use, but strongly
> > signals that re-use ought not to be done.
> >
> >>>> class Test:
> > ...     def _private(self): pass
> > ...
> >
>
>Python now have an "official" means to make attributes private by prefixing
>them with a double-underscore. And it *does* prevent their use or
>overloading:

I'm not criticising the double underscore prefix feature but it prevents 
neither, if you factor in the name mangling that supports the feature. See 
the following transcript from Python 2.2.1 command line:

 >>> class A:
...     def __private(self): print "class A private"
...     def printit(self): self.__private()
...
 >>> class B(A):
...     pass
...
 >>> class C(A):
...     def _A__private(self): print "class C private"
...
 >>> a1 = A()
 >>> a1.printit()
class A private
 >>> a1._A__private()
class A private
 >>> b1 = B()
 >>> b1.printit()
class A private
 >>> b1._A__private()
class A private
 >>> c1 = C()
 >>> c1.printit()
class C private
 >>> c1._A__private()
class C private






More information about the Python-list mailing list