Preventing class methods from being defined

Dan Sommers me at
Sun Jan 15 22:18:00 EST 2006

On Sun, 15 Jan 2006 18:41:02 -0800,
David Hirschfield <davidh at> wrote:

> I want a class that, when instantiated, only defines certain methods
> if a global indicates it is okay to have those methods. So I want
> something like:

> global allow
> allow = ["foo","bar"]

> class A:
>     def foo():
>         ...

>     def bar():
>         ...

>     def baz():
>         ...

> any instance of A will only have and but no a.baz()
> because it wasn't in the allow list.

> I hope that makes sense.

I think so, at least in the "I can implement that idea" sense, although
not the "why would you need such a strange animal" sense.  Since "class"
is an executable statement in Python, this ought to do it:

    allow = ["foo", "bar"]

    class A:

      if "foo" in allow:
        def foo( ):

      if "bar" in allow:
        def bar( ):

> Don't ask why I would need such a strange animal ...

Consider yourself not asked.


Dan Sommers

More information about the Python-list mailing list