save class

Gabriel Genellina gagsl-py2 at
Fri Jun 15 01:42:39 CEST 2007

En Thu, 14 Jun 2007 16:05:14 -0300, nik <nikbaer at> escribió:

> On Jun 13, 10:04 pm, Josiah Carlson <josiah.carl... at>
> wrote:
>> Gabriel Genellina wrote:
>> > En Wed, 13 Jun 2007 23:11:22 -0300, nik <nikb... at> 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.
>> > 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
> I am hoping to change the self.dict for each subclass. I realize that
> I could save self.dict to file and then load in different dicts each
> time I get a new instance of class. But I want to be able to make
> subclasses of map that each have different self.dict. Then when I need
> to use them, just import the module and use the specific dict, instead
> of having to keep track of a separate dictionary file. I am new to

As Josiah said, I still don't see why do you want a *subclass*. If the  
only difference between your "subclasses" is their dict, they're not  
subclasses but just Map *instances*.
Let's say, have a class Person, with attributes "name" and "email". If I  
want to represent two different persons, I would create two Person  
*instances*: Person(name="Gabriel", email="gagsl-py2 at ...") and  
Person(name="nik", email="nikbaer at ...")
Classes try to capture behavior and structure; instances contain state  
(very roughly said). One *could* use two subclasses here, and in certain  
circumstances it may be useful, but it's not the most common case.

> this, but I thought that this would be a regular thing to do in
> python, because people must make classes in the interactive console
> and then export them somehow for later use.

I've never done that. I only use the interactive interpreter for testing  
purposes, I never "develop" code inside the interpreter.

Gabriel Genellina

More information about the Python-list mailing list