tags:

views:

58

answers:

3

Hello,

I would like to keep a reference of the objects I've created in one script to use them in another script (without using shelve).

I would like something close to :

script 1

class Porsche(Car):
    """ class representing a Porsche """
    def __init__(self, color):      
        self.color = color


class Porsche_Container:
    # objects here 
    return objects

my_Porsche = Porsche(blue)
my_Porsche2 = Porsche(red) 

script2

for object in Porsche_Container:
    print object.color

rgds,

A: 

The best way to do this is explicitly to construct the set of objects that you want to access. It is possible to list e.g. all global variables defined in the other script, but not a good idea.


script1

...
porsche_container = { myPorsche1, myPorsche2 }

script 2

import script1
for porsche in script1.porsche_container:
    ...
katrielalex
SCript 1 could also yield objects to be iterable.
Guillaume Lebourgeois
@katrielalex Your answer seems great but I get an "SyntaxError : invalid syntax error" message.
Bruno
@Guillaume Lebourgeois This option looks appropriate too, could you give me an example please ?
Bruno
@Bruno: you are probably using Python 2.x, which doesn't have the curly-bracket set notation. Use a list instead (`[...]`).
katrielalex
@katrielalex If porsche_container is a dictionary, shouldn't it be written in the form porsche_container = {element1 : value1, element2 : value2) ?
Bruno
@katrielalex It works great, thanks !
Bruno
@Bruno `{a, b}` is a set literal and different from a dict.
delnan
@delnan Thanks for the piece of information !
Bruno
http://docs.python.org/py3k/reference/simple_stmts.html#grammar-token-yield_stmt
Guillaume Lebourgeois
A: 

Are these scripts going to be run in different processes? If not the solution is pretty straight forward (see katriealex's answer).

If they are going to run in different processes then you'll need more complex solutions involving inter process communication.

Manoj Govindan
@Manoj Govindan These scripts are to be run in the same process.
Bruno
A: 

I'm assuming you want only one process.

Use import, treat one 'script' as the main module and the other 'script' as a library module that you import. In my modified example script2.py is the main module and script1.py is the library.

script1.py

# -*- coding: utf-8 -*-

class Porsche(object):
 """ class representing a Porsche """
 def __init__(self, color):
            self.color = color

BLUE = 1
RED = 2

def make_porsche_list():
    return [Porsche(BLUE), Porsche(RED)]

script2.py

# -*- coding: utf-8 -*-

import script1

porsche_container = script1.make_porsche_list()

for object in porsche_container:
    print object.color

Output:

eike@lixie:~/Desktop/test> python script2.py
1
2
eike@lixie:~/Desktop/test> ls
script1.py  script1.py~  script1.pyc  script2.py  script2.py~
Eike
@Eike Thanks for the post, I'll try and see what approach is the more convenient.
Bruno
@Bruno -- they are the same approach!
katrielalex
Yes, my approach and katrielalex's approach are the same. However katrielalex's answer was so short, therefore I wanted to explain it a little more - but I got carried away and wrote two short scripts. Also using a function to create the data seemed to me a better design, in a real program the list wont be constant.
Eike