473,578 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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",__publi c__ #[i_a, i_b]
print "outer",__publi c__ #[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 1255

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

Similar topics

4
2052
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 looked like about 20:20 split on the following syntaxes: 1 def func(arg1, arg2, arg3) : function... 2 def func(arg1, arg2, arg3):...
17
1761
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. Previously, when looking at: some_python(code) and_some_more = stuff there was no need to look at the the first line in order to know what
8
1604
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 imaginable for Python 2.3. One trick is to use decorators to implement multimethods. A while ago Howard Stearns posted here a recipe to implement...
2
1699
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 to accept it would be to pacify the supporters of the proposal, and that just isn't a good enough reason in language design. However, it got...
0
1140
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 isn't allowed in XHTML. I've tried to remove this attribute by executing QuickSearchButton.Attributes = ""; or QuickSearchButton.Attributes =...
0
823
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
2085
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
1945
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 file when I import it? Or perhaps anyway to find the decorators by loading the file by other methods (with out simply parsing it by hand). ...
4
1421
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 through to the exception otherwise. This is just syntactic sugar. Example:
0
7779
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8126
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7874
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6524
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5343
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3786
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3801
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1391
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1114
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.