<div class="gmail_quote">On Fri, Sep 30, 2011 at 4:16 PM, nicky van foreest <span dir="ltr"><<a href="mailto:vanforeest@gmail.com">vanforeest@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Sorry to bug you, but I tried also the following:<br>
<br>
class Step(trellis.Component):<br>
duration = trellis.attr(0)<br>
<br>
def __init__(self, machine):<br>
self.machine = machine<br>
self.prevs = trellis.Set([])<br>
<br>
instead of<br>
<br>
class Step(trellis.Component):<br>
<div class="im"> prevs = trellis.make(trellis.Set)<br>
duration = trellis.attr(0)<br>
<br>
</div> def __init__(self, machine):<br>
self.machine = machine<br>
<br>
This also works (that is, I get the same schedule in both cases). Is<br>
there a difference between the two implementations?</blockquote><div><br></div><div>The reason that your code currently *appears* to work is because you're not changing the value of 'prevs' at runtime. <br><br>
If at some point you did, say, "someStep.prevs = trellis.Set(somedata)", your program would break because none of the listeners of someStep.prevs would notice the change. That's why you should always declare your attributes using trellis.* descriptors.<br>
<br>
The more "correct" (trellisthonic?) way to write your code above is:<br><br><font class="Apple-style-span" face="'courier new', monospace">
class Step(trellis.Component):<br>
duration = trellis.attr(0)<br>
prevs = trellis.make(trellis.Set)<br>
machine = None<br>
# No __init__ method necessary!<br></font><br>
That is, there's no reason to have an __init__ method at all, since trellis.Component() already takes keyword arguments and assigns them to attributes, as long as they are defined in the class.<br></div><div><br></div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">The second<br>
implementation sets prevs as a class variable, but this does not<br>
appear necessary (telling from implementation 1).<font color="#888888"><br></font></blockquote><div><br></div><div>Please note that 'prevs' is *not* a "class variable". It's a descriptor. That is, Step.prevs is a descriptor object, but someStep.prevs (where someStep is an instance of Step) will be a distinct trellis.Set() instance, unless overridden when created (e.g. via "someStep=Step(prevs=...)").<br>
</div></div>