0

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,

2
  • Hi Bruno, what is it that you want to achieve / how are you planning on using this? It may make it easier to help you with a little more information. Commented Aug 25, 2010 at 12:56
  • @MattH Thanks for the concern, fortunately Stack Overflow exists ! Commented Aug 25, 2010 at 13:05

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

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 !
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.