New module for method level access modifiers

Discussion in 'Python' started by TimeHorse, Oct 23, 2007.

  1. TimeHorse

    TimeHorse Guest

    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
    TimeHorse, Oct 23, 2007
    #1
    1. Advertising

  2. 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.
    Bruno Desthuilliers, Oct 23, 2007
    #2
    1. Advertising

  3. On 10/23/07, Bruno Desthuilliers
    <> wrote:
    > > Specifically, I have created 3 decorators named public, private and
    > > protected.

    >
    > Lord have mercy.


    +1 QOTW.

    --
    Cheers,
    Simon B.

    http://www.brunningonline.net/simon/blog/
    GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues
    Simon Brunning, Oct 23, 2007
    #3
  4. On Tue, Oct 23, 2007 at 08:54:52PM +0200, Bruno Desthuilliers wrote regarding Re: New module for method level access modifiers:
    >
    > 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.
    >


    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...
    J. Clifford Dyer, Oct 23, 2007
    #4
  5. J. Clifford Dyer a écrit :
    > On Tue, Oct 23, 2007 at 08:54:52PM +0200, Bruno Desthuilliers wrote
    > regarding Re: New module for method level access modifiers:
    >
    >> 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.
    >>

    >
    >
    > 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 !-)
    Bruno Desthuilliers, Oct 23, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shila
    Replies:
    3
    Views:
    671
    Tor Iver Wilhelmsen
    Feb 24, 2004
  2. Indudhar
    Replies:
    4
    Views:
    364
    Patrick May
    Oct 21, 2005
  3. Tony Morris

    Local Classes - Access Modifiers?

    Tony Morris, Jan 14, 2006, in forum: Java
    Replies:
    10
    Views:
    920
    Stefan Schulz
    Jan 16, 2006
  4. pabbu
    Replies:
    8
    Views:
    707
    Marc Boyer
    Nov 7, 2005
  5. Miquel

    Class & modifiers modifiers

    Miquel, Jan 25, 2007, in forum: Ruby
    Replies:
    4
    Views:
    160
    Miquel
    Jan 26, 2007
Loading...

Share This Page