# iterating initalizations

Aaron Stepp stepp.aaron at gmail.com
Tue Dec 23 16:39:52 CET 2008

```import random
from rtcmix import *
from chimes_source import * # Chime.play()
from rhythmblock import * # rhythmBlock.rhythmTwist() and
rhythmBlock.printStuff()
from pitchblock import * # pitchBlock.pitchTwist() and
pitchBlock.printStuff()
from lenEval import *  #greaterThan.sovler()

indexrand = random.Random()
indexrand.seed(2)

chime = Chime()
notes = pitchBlock()
rhythm = rhythmBlock()
solve = greaterThan()

class arrayBlock:

def __init__(self, theTempo, start):
self.__A = []
self.__B = []
self.__start = start
self.__tempo = theTempo

def player(self, length, tempo, octave, pan, seed):

tempo = (120, self.__tempo)

for a in range(length):

one = indexrand.randint(0, 3)

two = indexrand.randint(0, 7)

self.__A = self.__A + notes.pitchTwist(one , two)

for b in range(length):

one = indexrand.randint(0, 3)

two = indexrand.randint(0, 7)

self.__B = self.__B + rhythm.rhythmTwist(one , two)

lenA = len(self.__A)
lenB = len(self.__B)

var = solve.solver(lenA, lenB)

for c in range(var):

print self.__A[c]

self.__start = self.__start + tb(self.__B[var])

chime.play(self.__start, self.__A[var], octave, pan, seed)

This almost does exactly what I want, and is far cleaner than my
previous attempts.

The only problem is that now all my arguments are being passed as zeros!

I assume this has to do with WHEN I'm referencing self.__A and self.__B?

AS

On Dec 23, 2008, at 10:20 AM, Steve Holden wrote:

> D'Arcy J.M. Cain wrote:
>> On Mon, 22 Dec 2008 22:32:17 -0500
>> Aaron Stepp <stepp.aaron at gmail.com> wrote:
>>> Instead of writing a long list of initializations like so:
>>>
>>> A = [ ]
>>> B = [ ]
>>> ...
>>> Y = [ ]
>>> Z = [ ]
>>>
>>> I'd like to save space by more elegantly turning this into a
>>> loop.  If
>>
>> Well, if all you want is a loop:
>>
>>    for v in vars:
>>        locals()[v] = []
>>
> Note that this isn't guaranteed to work. While locals() will return a
> dict containing the names and values from the local namespace, you
> won't
> affect the local namespace by assigning values to the appropriate
> keys:
>
>>>> def f():
> ...   a = "hello"
> ...   locals()["a"] = "goodbye"
> ...   print a
> ...
>>>> f()
> hello
>>>>
>
> If you look at the function's code you will see that the local "a" is
> accessed using the LOAD_FAST and STORE_FAST opcodes, which take
> advantage of the knowledge that the name is local - the interpreter
> analyzed the function body looking for assignments to non-globals, and
> optimizes its treatment of such names.
>
>>>> dis.dis(f)
>  2           0 LOAD_CONST               1 ('hello')
>              3 STORE_FAST               0 (a)
>
>  3           6 LOAD_CONST               2 ('goodbye')
>             12 CALL_FUNCTION            0
>             18 STORE_SUBSCR
>
>  4          19 LOAD_FAST                0 (a)
>             22 PRINT_ITEM
>             23 PRINT_NEWLINE
>             27 RETURN_VALUE
>>>>
>
>> It's hard to tell if that's what you actually need though without
>> deeper analysis of your requirements.
>>
> I think it's unlikely that the OP really does need to create names
> dynamically, and should look at using either a dict indexed by the
> letters of self.__abet, or a list indexed from 0 to 24 instead. But
> you
>
> regards
> Steve
>
>
> --
> Steve Holden        +1 571 484 6266   +1 800 494 3119
> Holden Web LLC              http://www.holdenweb.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

```