references/pointers in Python?

Patrick K. O'Brien pobrien at orbtech.com
Thu Sep 13 18:36:04 EDT 2001


Do you have a good way of creating a Facade class that wraps immutable
attributes with the same simple dot syntax as the original class? Below is a
very simple example that *doesn't* work, that would appear to benefit if we
could create references. Class A is a class with many simple true/false
attributes. Class B is a Facade that simplifies the interface to A by only
exposing a few of its attributes. When the attributes are methods, or
mutable types, the following style of code works. But it fails in the
example below which has only immutable int types:

Welcome To PyCrust 0.6 - The Flakiest Python Shell
Python 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
Startup script executed: C:\Code\.pythonrc.py
>>> class A:
...     def __init__(self):
...         self.toggle01 = 0
...         self.toggle02 = 0
...         self.toggle03 = 0
...         self.toggle04 = 0
...         self.toggle05 = 0
...         self.toggle97 = 0
...         self.toggle98 = 0
...         self.toggle99 = 0
...
>>> class B:
...     def __init__(self, other):
...         self.toggle01 = other.toggle01
...         self.toggle02 = other.toggle02
...         self.toggle03 = other.toggle03
...
>>> a = A()
>>> dir(a)
['toggle01', 'toggle02', 'toggle03', 'toggle04', 'toggle05', 'toggle97',
'toggle98', 'toggle99']
>>> b = B(a)
>>> dir(b)
['toggle01', 'toggle02', 'toggle03']
>>> b.toggle01 = 1
>>> print b.toggle01
1
>>> print a.toggle01
0
>>>

I would love to see a solution where something like this would work, as I
have a need for exactly this kind of Facade class. Note that I do not want
to have to create get() and set() methods. I'd like access to immutable
types to transparently pass through class B. Any suggestions?
---
Patrick K. O'Brien
Orbtech (http://www.orbtech.com)
"I am, therefore I think."


"Alex Martelli" <aleax at aleax.it> wrote in message
news:9nqep802647 at enews2.newsguy.com...
> "Anton Lavrik" <asl at tepkom.ru> wrote in message
> news:mailman.1000327335.10232.python-list at python.org...
> > On Wed, Sep 12, 2001 at 12:06:06PM -0700, Cliff Wells wrote:
> > > On Wednesday 12 September 2001 10:09, you wrote:
> > >
> > > > And does this all mean that I should move on to Perl or any other
> > > > language, if I need effective implementation of references :) ?
>
> Why would you NEED them?  An example, please!  One that cannot
> be handled better with other tools.






More information about the Python-list mailing list