Preventing class methods from being defined

David Hirschfield davidh at ilm.com
Sun Jan 15 21:41:02 EST 2006


Here's a strange concept that I don't really know how to implement, but 
I suspect can be implemented via descriptors or metaclasses somehow:

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 a.foo() and a.bar() but no a.baz() 
because it wasn't in the allow list.

I hope that makes sense.
Don't ask why I would need such a strange animal, I just do. I'm just 
not sure how to approach it.

Should class A have some special metaclass that prevents those methods 
from existing? Should I override __new__ or something? Should those 
methods be wrapped with special property subclasses that prevent access 
if they're not in the list? What's a low-overhead approach that will 
work simply?

Thanks in advance,
-David

-- 
Presenting:
mediocre nebula.




More information about the Python-list mailing list