static object

Felipe Almeida Lessa felipe.lessa at gmail.com
Wed Jan 3 19:36:20 EST 2007


On 1/3/07, meelab <meelab at free.fr> wrote:
> I am looking for a way to create a "static object" or a "static class" -
> terms might be inappropriate - having for instance:

An example will speak better than me:

class Card(object):
    __cards = {}

    def __init__(self, number, suit):
        self.number, self.suit = number, suit

    def __new__(cls, number, suit):
        try:
            return cls.__cards[(number, suit)]
        except KeyError:
            obj = object.__new__(cls, number, suit)
            cls.__cartas[(number, suit)] = obj
            return obj


-- 
Felipe.



More information about the Python-list mailing list