James Stroud wrote: > John Salerno wrote: > >> Let's pretend I'm creating an Employee class, which I will later >> subclass for more specific jobs. Each instance will have stuff like a >> name, title, degrees held, etc. etc. >> >> So I'm wondering, is the best way to get all this information into the >> object to just have a really long __init__ method that takes each >> argument? >> >> Does a question like this depend on how the class will be used? I'm >> trying to construct it in a way that is independent of, say, the GUI >> interface that will be used, but I can't help but think that if all >> the data is entered into a GUI, then passed programmatically to the >> class, then it's no big deal because it happens behind the scenes. But >> if, for instance, someone manually created a new instance, they would >> have a ton of arguments to type in each time. Granted, even with the >> GUI they are basically typing in arguments, but in the manual case >> you'd have to type in the call to the class, the parentheses, commas, >> quotes around the strings, etc. (But still, I'm trying not to let a >> specific interface influence the construction of what should probably >> be a completely independent class implementation.) >> >> Thanks. > > > This assumes that someone will hard-code an instance of your Employee. > E.g.: > > e = Employee(Last, First, ID, > private_medical_info = 'Has halitosis.') > > Will this ever really happen--either by an API user or an end user? > Probably not for a database of any usefulness at all. Usually records > will be programatically created (or created through a gui), so it really > isn't necessary to worry about this case, except maybe for testing. This > has been my experience. Its probably better to init with the bare > minimum parameters and focus on how to move information from an external > source into an already created instance: > > e = Employee(Last, First, ID) > e.private_medical_info = 'Has halitosis.' > > If you are worried about validating field names, maybe use a method: > > class Employee(object): > def __init__(self, Last, First, ID, **kwargs): > self.Last = Last > self.First = First > self.ID = ID > self._fields = {'Last':'Last Name', > 'First':'First Name', > 'ID':'Employee Identification Number', > 'private_medical_info':'Any source of bad odor.'} > for (field, value) in kwargs.items(): > self.set_value(field, value) > def set_value(field, value): > if not field in self._fields: > raise ValueError, 'Field "%s" not supported.' % field > else: > self.__setattr__(field, value) > def get_value(field): > if not field in self._fields: > raise ValueError, 'Field "%s" not supported.' % field > else: > self.__getattribute__(field) > > > OK, I'll stop. You get the idea. > > You will find dealing with external sources the more interesting problem > anyway. > > James > > should be
def set_value(self, field, value): and def get_value(self, field): James -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo/python-list