Preventing class methods from being defined
David Hirschfield
davidh at ilm.com
Mon Jan 16 21:55:43 EST 2006
Thanks for this, it's a great list of the ways it can be done. Here's a
bit more insight into the arrangement I'm trying to get:
restrict = True
class A(object):
_restrict = ["test"]
def _null(self, *args, **kws):
raise Exception,"not allowed to access"
def test(self):
print "test restricted"
def __init__(self):
if restrict:
for f in self._restrict:
setattr(self,f,self._null)
class C(R):
def __init__(self):
super(C,self).__init__()
def test(self):
print "test from c"
In this design, calling c.test() where c is an instance of C will raise
an exception. Now, the only thing I'd like is to not have to fill out
that _restrict list like that, but to have some function or something
that let's me say which methods are restricted in the same way you
define class methods or properties, i.e.:
class A(object):
_restrict = []
def _null(self, *args, **kws):
raise Exception,"not allowed to access"
def test(self):
print "test restricted"
restrict(test)
#### this does some magic to insert "test" into the _restrict list
I can't really find a way to make that work with descriptors, and it
can't just be a function call, because I won't know what object to get
the _restrict list from. Is there a way to refer to the class that "is
being defined" when calling a function or classmethod?
So, ideas on how to accomplish that...again, greatly appreciated.
-Dave
Bengt Richter wrote:
>On Sun, 15 Jan 2006 19:23:30 -0800, David Hirschfield <davidh at ilm.com> wrote:
>
>
>
>>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?
>>
>>
>>
>Seems like you can
>a. define the class with all methods defined within, and use a metaclass to prune
> out the ones you don't want, which Alex provided.
>b. define the class with conditional execution of the method definitions, which you just rejected.
>c. define the class with no iffy methods at all, and add them afterwards
> c1. in a metaclass that adds them and possibly also defines them for that purpose
> c2. by plain statements adding method functions as class attributes
>d. define all the methods normally, but monitor attribute access on the class and raise
> attribute error for the methods that aren't supposed to be there.
>e. raise an exception conditionally _within_ methods that aren't supposed to be there, if called.
>
>What would you like?
>
>BTW, defining the method functions someplace other than within the body of the class whose methods
>they are to become has some subtleties, since the functions can potentially refer to different
>global scopes than that of the class (e.g. if you take the functions from an imported module)
>and/or use closure-defined cell variables (e.g. if the method function is defined within a factory function).
>This can be used to advantage sometimes, but needs good documentation to be clear for the next code maintainer ;-)
>
>I guess I should re-read your original requirements that led to thes design ideas.
>
>Regards,
>Bengt Richter
>
>
--
Presenting:
mediocre nebula.
More information about the Python-list
mailing list