iterating initalizations

Chris Rebert clp at rebertia.com
Mon Dec 22 18:19:49 EST 2008


On Mon, Dec 22, 2008 at 2:22 PM, Aaron Stepp <stepp.aaron at gmail.com> wrote:
> Hi all:
>
> I'm new to python and trying to save time and code by iterating through list
> initializations as well as the assignments.  I have the following code:
>
> import random
> from rtcmix import *
> from chimes_source import *
> from rhythmblock import *
> from pitchblock import *
>
> indexrand = random.Random()
> indexrand.seed(2)
>
> rhythm = rhythmBlock()
> pitch = pitchBlock()
>
> class pitchAndRhythm:
>
>        def __init__self:
>
>        self.__abet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>
>
> def listCreate(self, num):
>
>        if num > 25:
>
>                print "Oops.  This won't work"
>
>        else:
>
>                for a in range(num):
>
>                        b = indexrand.randint(0, 3)
>
>                        c = indexrand.randint(0, 7)
>

I don't quite understand what you're trying to do, but I can offer some advice.

>                        index = self.__abet[a]
The previous line is pointless currently. I don't understand what you
expect it to do, considering you completely overwrite 'index' in the
very next line.

>                        index = [ ]
>
>                        index = index.append(rhythm.rhythmTwist(b, c))
Important Note: .append() modifies the list *in-place*. It does *not*
return the modified list, it returns None. You probably want to
eliminate the 'index =' part of the previous line.

>
> This doesn't do what I expect (probably because I don't have a clue what I'm
> doing!): initalizing, then filling new arrays, each new one called A[ ],
> then B[ ], etc.

You probably want a dictionary of names to lists then, the names being
items of __abet and the lists being the corresponding 'index'.

It likely goes without saying, but you ought to read the fine tutorial as well.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list