473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Attribute access by hacking decorators

Here is a hack to implement attribute access (say for a Web framework)
the way I always wanted it to be. I say "hack" since it uses sys._getframe
and it sets the locals of a class, which is not recommended. Still, it seems
to work. I post the recipe here since since I would like to know if

1. it really works;
2. if yes, is there a chance that it will not work in future versions of Python;
3. will it work under Jython?

Even if it turns out it does not work, still I like the feeling of it ;-)
Here is the decorator which builds the list of the public methods in the
actual scope (it may be the module level scope or the class level scope):

import sys

def public(f):
loc = sys._getframe(1).f_locals
if not "__public__" in loc:
loc["__public__"] = [f]
else:
loc["__public__"].append(f)
return f

Here is the usage for global functions:

@public
def g_a():
pass

@public
def g_b():
pass

print __public__ # [g_a, g_b]

Here is the usage for methods:

class D(object):
@public
def m_a(self):
pass
@public
def m_b(self):
pass
print __public__ # [m_a, m_b]

It seems to work for nested classes too:

class Outer(object):
@public
def m_a(self):
pass
@public
def m_b(self):
pass
class Inner(object):
@public
def i_a(self):
pass
@public
def i_b(self):
pass
print "inner",__public__ #[i_a, i_b]
print "outer",__public__ #[m_a, m_b]

Of course, it does not work at the function scope, as in this example:

def container():
@public
def i_a(self):
pass
@public
def i_b(self):
pass
print "container", __public__ # [g_a, g_b] and not [i_a, i_b]

container()

However, I do not regard this as a problem, since I only want to define
public/private functions/methods at the module and class level, not at the
function level. In my experiments settings the locals at the class scope works,
but I wonder how reliable is this, wrt to future versions of Python or alternative
implementations.

Hacking-decorators-ly yours,

Michele Simionato
Jul 18 '05 #1
0 1243

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
17
by: daishi | last post by:
For what it's worth: As far as I know, the proposed @decorator syntax will be the first time that two logical lines of python with the same indentation will not be independent of one another....
8
by: Michele Simionato | last post by:
Decorators can generate endless debate about syntax, but can also be put to better use ;) Actually I was waiting for decorators to play a few tricks that were syntactically too ugly to be even...
2
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason...
0
by: Jonas | last post by:
Hi! I'm working with an ASP.NET web site that needs to conform with XHTML 1.0 Transitional and got an ASP.NET HtmlInputButton which automatically adds the attribute language="javascript" which...
0
by: Gert Cuykens | last post by:
would it not be nice if you could assign decorators to attributes too ? for example class C: @staticattribute data='hello' or class C:
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there anyway for me to find all the decorates declared in a...
4
by: MonkeeSage | last post by:
Proposal: When an attribute lookup fails for an object, check the top-level (and local scope?) for a corresponding function or attribute and apply it as the called attribute if found, drop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.