Preventing direct access to a class

Daniel Klein danielk at aracnet.com
Sun Feb 4 22:56:07 CET 2001


On 04 Feb 2001 18:16:25 +0000, Michael Hudson <mwh21 at cam.ac.uk> wrote:

>You won't be able to do this completely.  You can make it sufficiently
>hard that people won't do it by mistake, which is good enough for most
>folk (it is for me).
>
>Here's one way:
>
>-------------------------------
>class A:
> pass
>
>class B:
> def foo(self,A=A):
>  return A()
>
>del A
>-------------------------------
>
>the class A is still there:
>
>>>> B.foo.im_func.func_defaults 
>(<class __main__.A at 0x8195024>,)
>
>but I think you can rely on people not doing
>
>>>> B.foo.im_func.func_defaults[0]()
>
>accidentally <wink>.

Sneaky!

I actually resolved my own problem by shifting some of the responsibility of
B() back over to A() (where it belongs), so I don't have the problem anymore.
But thanks for the idea, I'll keep it on tap.

Dan



More information about the Python-list mailing list