I am trying to create a class similar to borg. I would like one specific property to be shared by all instances, but the other properties I would like to be unique to the instance. Here is what I still have:
class SharedFacility:
_facility = None
def __init__(self):
entries = {'facility': self._facility}
self.__dict__.update(entries)
def _getfacility(self):
return self._facility
def _setfacility(self, value):
self._facility = value
facility = property(_getfacility, _setfacility)
class Warning(SharedFacility):
def __init__(self, warnlevel, warntext):
SharedFacility.__init__(self)
print "Warning.__init__()"
self.warntext = warntext
self.warnlevel = warnlevel
def __call__(self):
self.facility(self.warnlevel, self.warntext)
def functionOne(a,b):
print 'functionOne: a=%s, b=%s' % (a,b)
def functionTwo(a,b):
print 'functionTwo: a=%s, b=%s' % (a,b)
w1 = Warning(1, 'something bad happened')
w1.facility = functionOne
w2 = Warning(5, 'something else bad happened')
import pdb; pdb.set_trace()
if w1.facility is w2.facility:
print "They match!"
w1()
w2()
w2.facility = functionTwo
if w1.facility is w2.facility:
print "They match!"
w1()
w2()
The code above does not work. I would like w1.facility and w2.facility to be a reference to the same object, but w1.warntext and w2.warntext two are two different values. I am working with python 2.4.3 (it makes no sense to mention that I am updating because I cannot).
Decision
class Warning(object):
_facility = None
def __init__(self, warnlevel, warntext):
print "Warning.__init__()"
self._warntext = warntext
self._warnlevel = warnlevel
def __call__(self):
Warning._facility(self._warnlevel, self._warntext)
def _getfacility(self):
return Warning._facility
def _setfacility(self, value):
Warning._facility = value
facility = property(_getfacility, _setfacility)
@staticmethod
def functionOne(a,b):
print 'functionOne: a=%s, b=%s' % (a,b)
@staticmethod
def functionTwo(a,b):
print 'functionTwo: a=%s, b=%s' % (a,b)
source
share