[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/unicode48/python-patterns/master/flyweight.py [Back]  [Original]

'''http://codesnipers.com/?q=python-flyweights'''

import weakref  

class Card(object):
    '''The object pool. Has builtin reference counting'''
    _CardPool = weakref.WeakValueDictionary() 

    '''Flyweight implementation. If the object exists in the
    pool just return it (instead of creating a new one)'''
    def __new__(cls, value, suit):         
        obj = Card._CardPool.get(value + suit, None)         
        if not obj:             
            obj = object.__new__(cls)             
            Card._CardPool[value + suit] = obj             
            obj.value, obj.suit = value, suit          
        return obj

    # def __init__(self, value, suit):         
    #     self.value, self.suit = value, suit      

    def __repr__(self):         
        return "" % (self.value, self.suit)      

if __name__ == '__main__':
    # comment __new__ and uncomment __init__ to see the difference
    c1 = Card('9', 'h')
    c2 = Card('9', 'h')
    print(c1, c2)
    print(c1 == c2)
    print(id(c1), id(c2))

Web Proxy Viewer  |  New URL  |  Original Page