[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] stumped by basic python



> On 1/6/06, Knapp <magick.crow@xxxxxxxxx> wrote:
>>  I want to rewrite this so that I can put it all in a loop calling on
>> the
>> data in the list to make the functions and fill in the data. What I
>> don't
>> know if how to come up with "sunModel" and the "mercuryModel" etc in the
>> loop based on the list names and then set the data to that. Make sense?

> You have a few options.
>
> d = {}
> for name, filename, scale in sol1:
>     model = loader.loadModelCopy("BasicPlanet2.egg")
>     model.reparentTo(render)
>     model.setScale(scale)
>     model.setTexture(loader.loadTexture(filename), 1)
>     d[name] = model
>
> This will give you a dictionary (d) which has keys which are the
> names, and values which are your models. There are lots of other
> solution. You might also want to look at help(setattr).

To try to help clarify that:

What he's suggesting is that you use Python's "dictionary" class for two
things: storing several pieces of data about each planet, and for storing
(references to) newly created model objects. You'd be storing planet data
in something like this format:

planet_data = [ {"name":"Mercury","orbit":.5,"graphic":"mercury.jpg"},
{"name":"Venus","orbit":.7,"graphic":"venus.jpg"} ]

That's a list of dictionaries in which each one has bits of data you can
access by name, eg:

[Input] planet_data[0]["orbit"]
[Output] .5

Look into the difference between that and "planet_data[0].get("orbit"),
which is safer since it returns None if the key doesn't exist and even
lets you pick a different default value.

Then you use the method above to create a bunch of objects whose names are
generated from the planet_data instead of you having to manually name
them. The key line there is "d[name] = model", which lets you forget about
"model" and henceforth refer to the newly-made planet by "d['Mercury']" or
"d.get('Mercury')" or whatever. If you weren't sticking that reference
into the dictionary d somewhere in that function, it's my understanding
that the new "model" would get destroyed at the end of that loop, but
Python will spare the model so long as _something_ keeps a reference to
it.

The way I might do it is something like:
models = {}
for planet in planet_data:
    planet_name = planet["name"]
    graphic = planet["graphic"]
    new_model = CreateFancyModelWithGraphics(graphic)
    models[ planet_name ] = new_model

Two things to watch out for are code that modifies the original
planet_data, and code that creates only a reference to something instead
of a genuinely new object. I call that second one the "stripy doom error"
because in displaying 2D tile graphics, having each row turn out to be
references to the same row means that when you think you're changing one
tile, you actually change a whole stripe of them. If you encounter
something like this, look at Python's copy.copy and copy.deepcopy.

Hope this helps.

Kris