[Tutor] yet another question on OO inheritance
Dave Angel
davea at dejaviewphoto.com
Wed Aug 19 00:54:26 CEST 2009
Serdar Tumgoren wrote:
> Thanks to you both for the suggestions. I think I'll try the approach
> below. But just one follow-up: should I be setting "self.tablename",
> or is a static attribute ("tablename") the correct approach?
>
>
>> A nice way to do this is with a class attribute. For example:
>> class Committee(object):
>>
>> def get_data(self):
>> sql =""
>> select id, name
>> from table_'%(tablename)s'
>> """ % {'tablename':self.tablename}
>>
>> class CandidateCommittee(Committee):
>> tablename =CandidateTable'
>>
>> PresidentialCommittee(Committee):
>> tablename =PresidentialTable'
>>
>> When you call Committee.get_data() it will get tablename from the
>> class of the instance being used.
>>
>>
>
>
(You top-posted. On this list, you should put your response at the end,
or inline if needed. It makes it much easier to follow the thread)
Since you presumaby want the value tablename to be the same for all
instances of a given class, you should use a class attribute, as Kent said.
DaveA
More information about the Tutor
mailing list