Newbie class instance tracking system

Michael McGovern mmcgover at pender.ee.upenn.edu
Wed May 9 21:57:27 CEST 2001


I am new to Python and I am having trouble with the following code.  In short,
Why doesn't it work?  I thought that variable names "spring into existence" in
a similar fasion to perl...


# I want to keep track of all the unique cars per company without repeating
#
# I get this error:
#Traceback (innermost last):
#  File "quest.py", line 14, in ?
#    prelude = Maker(carName='prelude',carCompany='Honda')
#  File "quest.py", line 11, in __init__
#    Maker.all_cars[carCompany].append(carName)
#KeyError: Honda

class Maker:
	all_cars={}

	def __init__(self,carName=None,carCompany=None):
		self.carName = carName
		self.carCompany = carCompany

		Maker.all_cars[carCompany].append(carName)
                # in perl, I would push(@{$all_cars{$carCompany}},$carName)

prelude = Maker(carName='prelude',carCompany='Honda')
accord = Maker(carName='accord',carCompany='Honda')		





Thanks in advance,
Scott
mmcgover at pender dot ee dot upenn dot edu



More information about the Python-list mailing list