@property

   1 class Orchestration(object):
   2     def __init__(self):
   3         self._choreo1 = None
   4         self._choreo2 = None
   5 
   6 
   7     @property
   8     def choreo1(self):
   9         if self._choreo1 is None:
  10             print 'Connecting to Choreo1'
  11             self._choreo1 = Choreo1()
  12 
  13         return self._choreo1
  14 
  15 
  16     @choreo1.setter
  17     def choreo1(self, value):
  18         msg = 'Setting to %s does not make sense in this example' % value
  19         raise AttributeError(msg)
  20 
  21 
  22     @choreo1.deleter
  23     def choreo1(self):
  24         raise AttributeError('Deleting neither')
  25 
  26 
  27     @property
  28     def choreo2(self):
  29         if self._choreo2 is None:
  30             print 'Connecting to Choreo2'
  31             self._choreo2 = Choreo2()
  32 
  33         return self._choreo2
  34 
  35 
  36     def bla(self):
  37         self.choreo1.foo()
  38         self.choreo1.bar()
  39         self.choreo2.gurki()
  40 
  41 
  42     def blubb(self):
  43         self.choreo2.gurki()
  44 
  45 
  46 
  47 class Choreo1(object):
  48     def foo(self):
  49         print 'Doing foo'
  50 
  51     def bar(self):
  52         print 'Doing bar'
  53 
  54 
  55 class Choreo2(object):
  56     def gurki(self):
  57         print 'Doing gurki'
  58 
  59 
  60 def main():
  61     o = Orchestration()
  62     print 'Do bla'
  63     o.bla()
  64     print 'Done with bla'
  65 
  66     print
  67 
  68     o = Orchestration()
  69     print 'Do blubb'
  70     o.blubb()
  71     print 'Done with blubb'
  72 
  73 
  74 if __name__ == '__main__':
  75     main()

PyUGAT: Property (last edited 2014-04-14 20:25:18 by Krille)