473,799 Members | 3,382 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 1269

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

Similar topics

4
2072
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): function...
17
1785
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
1607
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 generic functions a.k.a multimethods.
2
1708
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 pretty darn close! I'm impressed with how the community managed to pull together and face the enormous...
0
1146
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 = null; or QuickSearchButton.Attributes.Remove("language");
0
829
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
2106
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
1955
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). Basically what I'm looking for is a way to, given a python file, look through that file and find all the...
4
1433
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
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.