save class

Josiah Carlson josiah.carlson at sbcglobal.net
Thu Jun 14 01:04:48 EDT 2007


Gabriel Genellina wrote:
> En Wed, 13 Jun 2007 23:11:22 -0300, nik <nikbaer at gmail.com> escribió:
>> It would seem that I want to actually save the source code for the
>> class. I know that I could of course open up an editor and just make
>> it, but my ideal would be to have the base class, Map, be able to make
>> the sub-classes. I don't want the class definition. What I want is an
>> actual class that I could later import and use somewhere else. I am
>> planning to have each one of these map objects contain a different
>> dictionary and then be able to import the map into the application,
>> but have certain methods defined in the Map super-class to draw data
>> out of the specific map's specific dictionary. I hope that makes
>> sense.
>>
>> Something like,
>> class Map:
>>      dict = {}
>>      def DoSomething(self):
>>          pass
>>
>>      def MakeNewMapSubClass(self, newclassname):
>>          """ make a new file, newclassname.py that contains a new
>> class
>>              newclassname(Map) that inherits from base-class Map.
> 
> And are you sure you actually need different subclasses? Will you 
> construct them several instances of each subclass? From the above 
> description I feel you want just different Map *instances*, each with 
> its own dict, not different *subclasses*.

What you said, and that his solution sounds like a Java approach to the 
problem (subclass an abstract base class that calls specific methods on 
the subclass to "do the right thing").

To offer the OP source he can use...

class Map:
     def __init__(self):
         self.dict = {}
     def DoSomething(self):
         #do something with self.dict

Every instance gets a new dictionary.  Now, if he actually wants to 
change the behavior of the DoSomething method, of course then it would 
make sense to subclass Map.


  - Josiah



More information about the Python-list mailing list