By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

save class

P: n/a
nik
Hi,

I would like to create a class and then save it for re-use later. I
have tried to use pickle, but am not sure if that is right. I am
sorry, but I am new to python.

Basically, I have a class, Map. I want to be able to create new maps:
MapA, MapB... that have Map as the base class.

start with-
class Map:
pass

and then get a different class

class MapA(Map):
pass

that can be saved in a .py file for re-use

so far I thought that -
cls = new.classobj('MapA', (Map, ), {})
file = open('somefile', mode='w')
pickle.dump(cls, file)

-might work, but it didn't.... can anybody point me in the right
direction? I know that classes must get saved from the interactive
console, so I would think that it would be a standard thing to do.

Thank you,
Nik

Jun 14 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
En Wed, 13 Jun 2007 22:20:16 -0300, nik <ni*****@gmail.comescribió:
I would like to create a class and then save it for re-use later. I
have tried to use pickle, but am not sure if that is right. I am
sorry, but I am new to python.
Do you want to save the *source*code* of your class, or do you want to
save created *instances* -objects- of your classes to retrieve them later
(like a database)?
Basically, I have a class, Map. I want to be able to create new maps:
MapA, MapB... that have Map as the base class.

start with-
class Map:
pass

and then get a different class

class MapA(Map):
pass

that can be saved in a .py file for re-use
You just create the .py file with any text editor, containing the source
code for all your classes.
so far I thought that -
cls = new.classobj('MapA', (Map, ), {})
file = open('somefile', mode='w')
pickle.dump(cls, file)

-might work, but it didn't.... can anybody point me in the right
direction? I know that classes must get saved from the interactive
console, so I would think that it would be a standard thing to do.
This would try to save the *class* definition, which is usually not
required because they reside on your source files.
If this is actually what you really want to do, try to explain us exactly
why do you think so. Chances are that there is another solution for this.

--
Gabriel Genellina

Jun 14 '07 #2

P: n/a
nik
On Jun 13, 6:48 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 13 Jun 2007 22:20:16 -0300, nik <nikb...@gmail.comescribió:
I would like to create a class and then save it for re-use later. I
have tried to usepickle, but am not sure if that is right. I am
sorry, but I am new to python.

Do you want to save the *source*code* of your class, or do you want to
save created *instances* -objects- of your classes to retrieve them later
(like a database)?
Basically, I have a class, Map. I want to be able to create new maps:
MapA, MapB... that have Map as the base class.
start with-
class Map:
pass
and then get a different class
class MapA(Map):
pass
that can be saved in a .py file for re-use

You just create the .py file with any text editor, containing the source
code for all your classes.
so far I thought that -
cls = new.classobj('MapA', (Map, ), {})
file = open('somefile', mode='w')
pickle.dump(cls, file)
-might work, but it didn't.... can anybody point me in the right
direction? I know that classes must get saved from the interactive
console, so I would think that it would be a standard thing to do.

This would try to save the *class* definition, which is usually not
required because they reside on your source files.
If this is actually what you really want to do, try to explain us exactly
why do you think so. Chances are that there is another solution for this.

--
Gabriel Genellina
Thanks for the response.

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.

Thanks,
Nik

Jun 14 '07 #3

P: n/a
En Wed, 13 Jun 2007 23:11:22 -0300, nik <ni*****@gmail.comescribió:
On Jun 13, 6:48 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Wed, 13 Jun 2007 22:20:16 -0300, nik <nikb...@gmail.comescribió:
I would like to create a class and then save it for re-use later. I
have tried to usepickle, but am not sure if that is right. I am
sorry, but I am new to python.

Do you want to save the *source*code* of your class, or do you want to
save created *instances* -objects- of your classes to retrieve them
later (like a database)?
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*.

--
Gabriel Genellina

Jun 14 '07 #4

P: n/a
Gabriel Genellina wrote:
En Wed, 13 Jun 2007 23:11:22 -0300, nik <ni*****@gmail.comescribió:
>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
Jun 14 '07 #5

P: n/a
nik
On Jun 13, 10:04 pm, Josiah Carlson <josiah.carl...@sbcglobal.net>
wrote:
Gabriel Genellina wrote:
En Wed, 13 Jun 2007 23:11:22 -0300, nik <nikb...@gmail.comescribió:
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
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
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.

Thank you for your responses.

Jun 14 '07 #6

P: n/a
En Thu, 14 Jun 2007 16:05:14 -0300, nik <ni*****@gmail.comescribió:
On Jun 13, 10:04 pm, Josiah Carlson <josiah.carl...@sbcglobal.net>
wrote:
>Gabriel Genellina wrote:
En Wed, 13 Jun 2007 23:11:22 -0300, nik <nikb...@gmail.comescribió:
>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@...") and
Person(name="nik", email="nikbaer@...")
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

Jun 14 '07 #7

P: n/a
nik wrote:
of having to keep track of a separate dictionary file. I am new to
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.
Create a file. Put your code in it. Run your code. Occasionally
copy/paste your code into the console for testing and/or learning about
how Python works. If you write something you want to keep in the
console, copy it out of the console and paste it into your source file(s).

- Josiah
Jun 15 '07 #8

P: n/a
of having to keep track of a separate dictionary file. I am new to
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.
No. That's not how things work. One does dabble with the interpreter a
bit, sometimes even creating a tiny class.

But except from the occasional expression tested in the interpreter,
it's not common to use that code and save it.

Write python-files from the start. Then either execute them

python myfile.py

or if you insist, do
python
>>import myfile
myfile.some_function()

Diez
Jun 15 '07 #9

P: n/a
nik
Thank you for all the responses. In light of what you've told me I
have gone back to storing my specific dictionaries in text files and
then reading them in to the class.

Thank you,
Nik
Jun 19 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.