A new metaclass pattern, class methods
Costas Menico
costas at meezon.com
Thu May 24 14:55:11 EDT 2001
Thomas,
I believe I may have a simpler. Please feel free to comment on the
code below.
costas
"""
Example from the animal kingdom (Penguins are birds, birds and dogs
are animals.)
to show how to make class methods, class variables and
instance methods, instance variables.
Based on previous discussions and ideas from the Python newsgroup.
Conventions used:
o All class classes end with Meta. E.g. ClassMeta
o Before using you must create your meta class object.
There should be only one instance (singleton)
of this in the system. E.g. Class = ClassMeta()
o To create an instance of a class you use the Class.New(args).
You can create as many instances as you need
o Future fix: Create a global class Class that tracks singleton
Class instances plus other default behavior in a global dictionary.
Steps to make your own classes.
1) Define a class name that ends with Meta. Inherit if necessary from
<superMeta>
2) Define an nested class called New. Inherit <superMeta>.New (if
necesary)
Don't forget to initialize the superclass in your __init__ and
if you have a destructor destroy() to also call the superclass.
3) Code your class methods and variables inside Meta
4) Code your instance methods and variables inside New
5) Create a singleton of your Meta class.
6) Use this to create as many objects as needed.
Author: costas menico / costas at meezon.com
"""
class AnimalMeta:
totalAnimals=0 # class variables
allAnimals = 'Are mobile'
def getInstances(self):
return AnimalMeta.totalAnimals
def name(self):
return 'animal class'
class New:
def __init__(self, name='animal instance'):
AnimalMeta.totalAnimals+=1
self.name = name
def name(self):
return self.name
def destroy(self):
AnimalMeta.totalAnimals-=1
class BirdMeta(AnimalMeta):
totalBirds=0 #class variables
allBirds = 'Have wings'
def name(self):
return 'bird class'
class New(AnimalMeta.New):
def __init__(self, name='bird instance'):
AnimalMeta.New(name) # initialize superclass
BirdMeta.totalBirds+=1
self.name = name
def destroy(self):
AnimalMeta.New.destroy(self)
BirdMeta.totalBirds-=1
def name(self):
return self.name
class DogMeta(AnimalMeta):
totalDogs=0 #class variables
allDogs = 'Bark'
def name(self):
return 'dog class'
class New(AnimalMeta.New):
def __init__(self, name='dog instance'):
AnimalMeta.New(name) # initialize superclass
DogMeta.totalDogs+=1
self.name = name
def destroy(self):
AnimalMeta.New.destroy(self)
DogMeta.totalDogs-=1
def name(self):
return self.name
def bark(self):
return 'woof'
class PenguinMeta(BirdMeta):
totalPenguin = 0 # class variables
allPenguins = 'Live in cold climate'
def name(self):
return 'penguin class'
class New(BirdMeta.New):
def __init__(self, name='penguin instance'):
BirdMeta.New(name) # initialize superclass
PenguinMeta.totalPenguin+=1
self.name = name
def destroy(self):
BirdMeta.New.destroy(self)
PenguinMeta.totalPenguin-=1
def name(self):
return self.name
def song(self):
return 'Penguins rock'
# Metaclass creation. Required before creating classes.
# You should only have one of each Meta.
Animal=AnimalMeta()
Bird=BirdMeta()
Dog=DogMeta()
Penguin=PenguinMeta()
#--------------------------------------------------------
# Tests
# Object instantation. Here do anything you want.
aMommaPenguin=Penguin.New('momma')
aPappaPenguin=Penguin.New('pappa')
aBabyPenguin=Penguin.New('baby')
aDog=Dog.New('fido')
#print some info
print aMommaPenguin.name
print aPappaPenguin.name
print aBabyPenguin.name
print aBabyPenguin.song()
print aDog.name
print 'Tot Penguins:', Penguin.totalPenguin
print 'Tot Animals:', Penguin.totalAnimals
print 'Tot Birds: ', Bird.totalBirds
print 'Tot Animals: ', Bird.totalAnimals
print 'Tot Dogs:', Dog.totalDogs
print 'Tot Animals:', Dog.totalAnimals
print 'Total Animals:', Animal.totalAnimals
#reduce animals and dogs by one
aDog.destroy()
del aDog
print 'Tot Animals:', Dog.totalAnimals
print 'Tot Dogs:', Dog.totalDogs
del aMommaPenguin,aPappaPenguin,aBabyPenguin
del Animal, Bird, Dog, Penguin
More information about the Python-list
mailing list