Re: trellis attrs

PJ Eby <pje-Wh6+Hckhi6HFNGf7iClzIwC/[email protected]> Fri, 30 Sep 2011 17:17:42 -0400
Newsgroups gmane.comp.python.peak
Message-ID <CALeMXf4ZiCvHQwq1AHyPMvcnyryt9e2sBm=dQ8tq+5+=ffNKwQ@mail.gmail.com>
--===============1281906949==
Content-Type: multipart/alternative; boundary=20cf30780da6e4bd7404ae2f272d

--20cf30780da6e4bd7404ae2f272d
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Sep 30, 2011 at 4:16 PM, nicky van foreest <[email protected]>wrote:

> Sorry to bug you, but I tried also the following:
>
> class Step(trellis.Component):
>    duration = trellis.attr(0)
>
>    def __init__(self, machine):
>        self.machine = machine
>        self.prevs = trellis.Set([])
>
> instead of
>
> class Step(trellis.Component):
>     prevs = trellis.make(trellis.Set)
>    duration = trellis.attr(0)
>
>     def __init__(self, machine):
>        self.machine = machine
>
> This also works (that is, I get the same schedule in both cases). Is
> there a difference between the two implementations?


The reason that your code currently *appears* to work is because you're not
changing the value of 'prevs' at runtime.

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.

The more "correct" (trellisthonic?) way to write your code above is:

class Step(trellis.Component):
    duration = trellis.attr(0)
    prevs = trellis.make(trellis.Set)
    machine = None
    # No __init__ method necessary!

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.



> The second
> implementation sets prevs as a class variable, but this does not
> appear necessary (telling from implementation 1).
>

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

--20cf30780da6e4bd7404ae2f272d
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div class=3D"gmail_quote">On Fri, Sep 30, 2011 at 4:16 PM, nicky van foree=
st <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]">vanforeest=
@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=
=3D"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>
 =A0 =A0duration =3D trellis.attr(0)<br>
<br>
 =A0 =A0def __init__(self, machine):<br>
 =A0 =A0 =A0 =A0self.machine =3D machine<br>
 =A0 =A0 =A0 =A0self.prevs =3D trellis.Set([])<br>
<br>
instead of<br>
<br>
class Step(trellis.Component):<br>
<div class=3D"im"> =A0 =A0prevs =3D trellis.make(trellis.Set)<br>
 =A0 =A0duration =3D trellis.attr(0)<br>
<br>
</div> =A0 =A0def __init__(self, machine):<br>
 =A0 =A0 =A0 =A0self.machine =3D 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 y=
ou&#39;re not changing the value of &#39;prevs&#39; at runtime. <br><br>

If at some point you did, say, &quot;someStep.prevs =3D trellis.Set(somedat=
a)&quot;, your program would break because none of the listeners of someSte=
p.prevs would notice the change.=A0 That&#39;s why you should always declar=
e your attributes using trellis.* descriptors.<br>
<br>
The more &quot;correct&quot; (trellisthonic?) way to write your code above =
is:<br><br><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, =
monospace">
class Step(trellis.Component):<br>
=A0=A0=A0 duration =3D trellis.attr(0)<br>
=A0=A0=A0 prevs =3D trellis.make(trellis.Set)<br>
=A0=A0=A0 machine =3D None<br>
=A0=A0=A0 # No __init__ method necessary!<br></font><br>
That is, there&#39;s no reason to have an __init__ method at all, since tre=
llis.Component() already takes keyword arguments and assigns them to attrib=
utes, as long as they are defined in the class.<br></div><div><br></div>
<div>=A0</div><blockquote class=3D"gmail_quote" style=3D"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=3D"#888888"><b=
r></font></blockquote><div><br></div><div>Please note that &#39;prevs&#39; =
is *not* a &quot;class variable&quot;.=A0 It&#39;s a descriptor.=A0 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 over=
ridden when created (e.g. via &quot;someStep=3DStep(prevs=3D...)&quot;).<br=
>
</div></div>

--20cf30780da6e4bd7404ae2f272d--

--===============1281906949==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
PEAK mailing list
[email protected]
http://www.eby-sarna.com/mailman/listinfo/peak
--===============1281906949==--