473,387 Members | 1,553 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.

automatic delegation

Is it possible to write a metaclass or something so that
with a class definition not much larger than this:

class foo:
def __init__(self, val): self.val = val

operations on a foo instance f will be applied to f.val?
E.g. str(f) -> f.__str__() -> f.val.__str__().

--
Hallvard
Jul 18 '05 #1
4 1467
>>>>> "h.b.furuseth" == Hallvard B Furuseth <h.**********@usit.uio.no> writes:

h.b.furuseth> Is it possible to write a metaclass or something so that
h.b.furuseth> with a class definition not much larger than this:

h.b.furuseth> class foo:
h.b.furuseth> def __init__(self, val): self.val = val

h.b.furuseth> operations on a foo instance f will be applied to f.val?
h.b.furuseth> E.g. str(f) -> f.__str__() -> f.val.__str__().

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52295

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #2
Hallvard B Furuseth wrote:
Is it possible to write a metaclass or something so that
with a class definition not much larger than this:

class foo:
def __init__(self, val): self.val = val

operations on a foo instance f will be applied to f.val?
E.g. str(f) -> f.__str__() -> f.val.__str__().


I tried in vain to solve your problem while improving my understanding of
metaclasses. I have made an observation that might interest you, though:
str(foo()) and foo.__str__(foo()) are not the same:
class Type(type): .... def __getattribute__(self, name):
.... def tostr(self):
.... return "so what"
.... return tostr
.... class C: .... __metaclass__ = Type
.... print str(C()) <__main__.C object at 0x40296a8c> print C.__str__(C()) so what


Care to explain, anyone?

Peter

Jul 18 '05 #3
Hallvard B Furuseth wrote:
Is it possible to write a metaclass or something so that
with a class definition not much larger than this:

class foo:
def __init__(self, val): self.val = val

operations on a foo instance f will be applied to f.val?
E.g. str(f) -> f.__str__() -> f.val.__str__().


Here a simple example :

class proxy(object):
def __init__(self, obj):
self.obj = obj

def __getattr__(self, key):
if key in self.__dict__:
return self.__dict__[key]
else:
return getattr(self.obj, key)

You can also look at:
http://www.python.org/cgi-bin/moinmoin/ProxyProgramming

--
Yermat

Jul 18 '05 #4
Yermat wrote:
Hallvard B Furuseth wrote:
Is it possible to write a metaclass or something so that
with a class definition not much larger than this:

class foo:
def __init__(self, val): self.val = val

operations on a foo instance f will be applied to f.val?
E.g. str(f) -> f.__str__() -> f.val.__str__().
Here a simple example :

class proxy(object):
def __init__(self, obj):
self.obj = obj

def __getattr__(self, key):
if key in self.__dict__:
return self.__dict__[key]
else:
return getattr(self.obj, key)


I tried __getattr__ before I posted, but it doesn't work:
int(proxy(5)) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: int() argument must be a string or a number

However, the example in the Cookbook works. Thanks, Ville.
Turns out the above example also works if one one removes 'object'
and makes it and old-style class:
int(proxy(5)) 5 str(proxy(5))

'5'

Seems that with new-style classes, __getattr__ only works for instance
variables, while for old-style classes, it also works for class
variables like __int__ and __str__. But I can't see anything in the
documentation which says so?
You can also look at:
http://www.python.org/cgi-bin/moinmoin/ProxyProgramming


That example doesn't work at all. The initialization of self._subject
in __init__() breaks because of the definition of __setattr__.

--
Hallvard
Jul 18 '05 #5

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

Similar topics

7
by: Rene Pijlman | last post by:
Section 6.5 "What is delegation?" of the FAQ says: "Python programmers can easily implement delegation. For example, the following class implements a class that behaves like a file but converts...
3
by: Jacob | last post by:
Hello All, I am trying to serve out some content via IIS that is hosted on a remote fileserver, and am unable to get the delegation working correctly. Our setup is as follows: Local LAN...
6
by: DPfan | last post by:
Is the following so-called "delegation"? If not how to make some changes so that the F class delegates its operation to an E instance. On the other hand the following code runs without any...
3
by: Tony Johansson | last post by:
Hello! What does it mean with delegation and can you give me one example. //Tony
2
by: | last post by:
Hi, I am working on a n-tier app using remoting. I am using the VS 2005 beta 2. My server needs to access a remote resources on behalf on the connected user. I have configured my server like :...
2
by: russell.lane | last post by:
I'm building out a pretty standard n-tier web application. The stack includes application/presentation, biz logic, and data access layers on top of an SQL server back end. We want to use...
4
by: JimLad | last post by:
In advance, sorry if this is the wrong group... SQL Server 2000 SP3 on Server 2003. SQL Account and Computer both Trusted for Delegation. Given SPN. IIS 5.0 on W2000. Kerberos enabled....
6
by: Marc Castrechini | last post by:
This is a classic double hop delegation issue, however its the first time we are setting this up so we are doing something incorrectly. If we run through the IDE or using a localhost path on the...
3
by: Patrick | last post by:
Hello I have the following scenario - SQL 2005 server (serversql) - Windows 2003 with IIS (serveriis) - Windows 2003 ADS (serverads) I want to connect to an intranet application using NTML...
5
by: =?Utf-8?B?TWF5ZXI=?= | last post by:
Hi, I'm using two form classes and I would like all methods of the second class (the child class) to be managed by the first class (the main class). Is delegation the best solution for me? If so,...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.