473,383 Members | 1,876 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,383 software developers and data experts.

Decorator pattern for new-style classes ?

Searching the archives for something related to the title, I found a
few relevant threads (e.g. http://tinyurl.com/avyg6 or
http://tinyurl.com/b5b6v); however they don't seem to give a
satisfactory answer, so here it goes again: What's the equivalent
new-style Delegate class ?

class Delegate:
def __init__(self, principal):
self._principal = principal

def __getattr__(self,name):
return getattr(self._principal,name)

# overriden methods follow here

This delegates both normal and __special__ methods; unfortunately for
new style classes __getattr__ is not called for missing special
methods. The workarounds I saw or can think of are so ugly that I
prefer to to use an old-style class after a long time :-/

George

Jul 19 '05 #1
1 1531
I have no time for a long discussion, but the code should
speak for itself:

class Container(object):
def __init__(self, content):
self.content = content
def __str__(self):
return "<Container containing %r>" % self.content

class Wrapped(object):
def __init__(self, obj):
self._obj = obj
def __getattribute__(self, name):
obj = super(Wrapped, self).__getattribute__("_obj")
return getattr(obj, name)

w = Wrapped(Container("hello"))

print w.content
print w.__str__() # works
print w # does not work as you would expect, see bug report SF 729913

The discussion around the bug report is worth reading,

Michele Simionato

Jul 19 '05 #2

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

Similar topics

41
by: John Marshall | last post by:
How about the following, which I am almost positive has not been suggested: ----- class Klass: def __init__(self, name): self.name = name deco meth0: staticmethod def meth0(x):
38
by: Lasse Vågsæther Karlsen | last post by:
After working through a fair number of the challenges at www.mathschallenge.net, I noticed that some long-running functions can be helped *a lot* by caching their function results and retrieving...
13
by: Lad | last post by:
I use Python 2.3. I have heard about decorators in Python 2.4. What is the decorator useful for? Thanks for reply L.
5
by: Doug | last post by:
I am looking at using the decorator pattern to create a rudimentary stored proc generator but am unsure about something. For each class that identifies a part of the stored proc, what if I want to...
1
by: Doug | last post by:
I am looking at using the decorator pattern to create a rudimentary stored proc generator but am unsure about something. For each class that identifies a part of the stored proc, what if I want to...
3
by: Gregory | last post by:
I recently reviewed the decorator pattern in the GOF book and noticed a problem. Let look at the example given in the book. For simplicity I removed intermediate Decorator class. // Interface...
3
by: Diego Jancic | last post by:
Hi! Hope you can help my with you issue... I made a domain model (http://cedev.com.ar/model.jpg) and what I'm trying to is the NHibernate mappings to persist it. (It's uses the Decorator Pattern,...
9
by: Tyno Gendo | last post by:
Hi I'm trying to learn patterns, which I hope to use in my PHP code, although I'm finding it hard to get any real impression of how patterns fit in properly, I've done the following test code...
8
by: Chris Forone | last post by:
hello group, is there a possibility to implement the decorator-pattern without new/delete (nor smartpt)? if not, how to ensure correct deletion of the objects? thanks & hand, chris
5
by: proxyuser | last post by:
The context of this question is actually from the book "C# 3.0 Design Patterns" (Bishop). She makes the point that one of the reasons you'd use Decorator is if you can't change the original...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.