Preventing class methods from being defined
David Hirschfield
davidh at ilm.com
Sun Jan 15 22:23:30 EST 2006
I should have explicitly mentioned that I didn't want this particular
solution, for a number of silly reasons.
Is there another way to make this work, without needing to place an
explicit "if allowed" around each method definition?
Thanks again,
-David
Dan Sommers wrote:
>On Sun, 15 Jan 2006 18:41:02 -0800,
>David Hirschfield <davidh at ilm.com> 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 a.foo() and a.bar() 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.
>
>HTH,
>Dan
>
>
>
--
Presenting:
mediocre nebula.
More information about the Python-list
mailing list