New module for method level access modifiers

T

TimeHorse

I have started work on a new module that would allow the decoration of
class methods to restrict access based on calling context.
Specifically, I have created 3 decorators named public, private and
protected.

These modifiers work as follows:

Private: A private method can only be called from a method defined in
the same class as the called method.

Protected: A protected method can only be called from a method defined
in the same or class derived from the called method's class.

Public: No access restrictions (essentially a no-op).

Programmers with a C++ or Java background will be familiar with these
concepts in those languages; these decorators attempt to emulate their
behavior

Bugs:

1) These decorators will not tolerate other decorators because they
examine the call stack in order to determine the caller's frame. A
second decorator, either before or after the access decorator will
insert a stack frame, which the current version of these decorators
cannot handle. Making sure decorators set their wrapper functions
__name__, __doc__ and append to its dictionary would be required at
least for interoperability.

2) As noted, staticmethod and classmethod cannot be handled by the
access decorators, not only because they are decorators themselves,
but because the current access decorators require access to an
instance of the class (self) in order to do method resolution.
classmethod support could probably be added without too much
difficulty but staticmethods, because they have no self or cls, would
be difficult.

3) Friend classes, as defined in C++. These would probably be defined
as a class-level list of classes that may have private/protected
access to the given class's internals. This should not be too hard to
add.

4) Decorators for member variables -- these decorators can already be
applied to get_* and set_* methods for properties. Overriding
__getattr__ may be a better solution for attribute access, however.

Source available at: http://starship.python.net/crew/timehorse/access.py

Jeffrey
 
B

Bruno Desthuilliers

TimeHorse a écrit :
I have started work on a new module that would allow the decoration of
class methods to restrict access based on calling context.
Specifically, I have created 3 decorators named public, private and
protected.

Lord have mercy.
 
J

J. Clifford Dyer

TimeHorse a ?crit :

Lord have mercy.

I invented a new decorator too. It gets rid of many of the limitations of python, including duck typing and hideously flat namespaces. It's used kind of like this:

@java
def public_com.sun.lord.have.mercy():
pass

Implementation forthcoming...
 
B

Bruno Desthuilliers

J. Clifford Dyer a écrit :
I invented a new decorator too. It gets rid of many of the
limitations of python, including duck typing and hideously flat
namespaces. It's used kind of like this:

@java
> def public_com.sun.lord.have.mercy():
> pass

Implementation forthcoming...

keyboard !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top