references/pointers in Python?

Ignacio Vazquez-Abrams ignacio at openservices.net
Fri Sep 14 00:53:51 CEST 2001


On Thu, 13 Sep 2001, Patrick K. O'Brien wrote:

> 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:
  facades=('toggle01', 'toggle02', 'toggle03')
  __members__=list(facades)
  def __init__(self, other):
    self.other=other
  def __setattr__(self, key, val):
    if key in self.facades:
      setattr(self.other, key, val)
    else:
      self.__dict__[key]=val
  def __getattr__(self, key):
    if key in self.facades:
      return getattr(self.other, key)
    else:
      return self.__dict__[key]

> >>> a = A()
> >>> dir(a)
> ['toggle01', 'toggle02', 'toggle03', 'toggle04', 'toggle05', 'toggle97',
> 'toggle98', 'toggle99']
> >>> b = B(a)
> >>> dir(b)

['other', 'toggle01', 'toggle02', 'toggle03']

> >>> b.toggle01 = 1
> >>> print b.toggle01
> 1
> >>> print a.toggle01

1

> >>>

Doncha just love Python? ;)

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>







More information about the Python-list mailing list