tags:

views:

24

answers:

2

Hello,

This post is the follow-up of my previous post (Create and retrieve object list in Python).

I had to modify my code in the following way :

script1

#!/usr/bin/python

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


def create_porsche(parameter_1, parameter_2):
    myPorsche = Porsche(color = parameter_1)
    myPorsche2 = Porsche(color = parameter_2) 

create_porsche(parameter_1 = 'blue', parameter_2 = 'red')
porsche_container = (myPorsche, myPorsche2)

and I'd like to have porsche_container = (myPorsche, myPorsche2) working the same way as in my previous script :

old script 1

#!/usr/bin/python

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


myPorsche = Porsche(color = 'blue')
myPorsche2 = Porsche(color = 'red') 


porsche_container = (myPorsche, myPorsche2)

How can I do that please ?

rgds,

+1  A: 

create_porsche doesn't return anything, so you don't know what it's created. Make it return a list of the cars that it creates, which you can then store in your global variable.

def create_porsche(parameter_1, parameter_2):
    myPorsche = Porsche(color = parameter_1)
    myPorsche2 = Porsche(color = parameter_2) 

    return [ myPorsche, myPorsche2 ]

porsche_container = create_porsche(parameter_1 = 'blue', parameter_2 = 'red')

The reason the code you posted above doesn't work is that the variables myPorsche and myPorsche2 are defined within the function create_porsche, so they are scoped to that function. That means that you can't see or access them outside of that block of code. If you want to know about them, make create_porsche return them. (Note: it is possible to tell Python that they should be global variables i.e., not scoped within the function -- you use the global keyword -- but you shouldn't do that unless you must.)


I don't mean to be rude here, but have you read any Python tutorials? Something like Dive Into Python might help you a lot in understanding things like this (function scopes and return values).

katrielalex
@katrielalex Thanks for the answer. Actually I'm really short of time right now but as soon as possible I'll sleep with Dive under my pillow !
Bruno
A: 

Here is my take on how to go about creating Porsche objects and add them to a container.

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

class PorscheCreator(object):
    def __init__(self):
        self._cars = []
    def create(self, *args, **kwargs):
        porsche = Porsche(*args, **kwargs)
        self._cars.append(porsche)
        return porsche
    def _get_cars(self):
        for each in self._cars:
            yield each
    cars = property(_get_cars)

creator = PorscheCreator()
myPorsche = creator.create('blue')
myPorsche2 = creator.create('red')     

And in script 2:

from script1 import creator

for each in creator.cars:
     print car.color
Manoj Govindan
@ Manoj Govindan Thanks for your help !
Bruno