<div dir="ltr">Excellent.  I guess I never read through the class stuff in learning python(its a great book, but very detailed....) Now I know better! </div><div class="gmail_extra"><br clear="all"><div>--<br><a href="http://about.me/greggmartinson" target="_blank">http://about.me/greggmartinson</a></div>

<br><br><div class="gmail_quote">On Fri, Apr 11, 2014 at 3:32 AM, Peter Otten <span dir="ltr"><<a href="mailto:__peter__@web.de" target="_blank">__peter__@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="">Gregg Martinson wrote:<br>
<br>
> I have been working through a fairly simple process to teach myself python<br>
> and I am running into a problem with a comparison.  Can anyone tell me<br>
> where I am going wrong?<br>
><br>
> #!/usr/bin/env python<br>
><br>
> class Team(object):<br>
>     code = ""<br>
>     opponents_debated=[]<br>
>     wins=0<br>
>     losses=0<br>
>     competitors=[]<br>
<br>
</div>Defining the 'competitors' list here means that it is shared by all Team<br>
instances. As soon as any team A has debated with a team B B is added to<br>
this list. As any team immediately adds itself to the list no debate will<br>
ever take place.<br>
<br>
Solution: instead of a class attribute make the list an instance attribute<br>
by moving the definition into the initialiser:<br>
<br>
><br>
>     def __init__(self, code):<br>
          self.competitors = []<br>
<div class="">>         self.code = code<br>
>         self.competitors.append(code)<br>
>         #self.school_teams.append(code)<br>
<br>
</div>Note that the difference between class and instance attributes exists for<br>
all attributes, but may not lead to an error when you rebind instead of<br>
mutating the attribute:<br>
<br>
>>> class T:<br>
...     wins = 0<br>
...     def win(self):<br>
...             self.wins = self.wins + 1<br>
...<br>
>>> a = T()<br>
>>> b = T()<br>
>>> a.win()<br>
>>> a.win()<br>
>>> b.win()<br>
>>> a.wins<br>
2<br>
>>> b.wins<br>
1<br>
>>> T.wins<br>
0<br>
<br>
That is because the first time win() is called on an instance<br>
<br>
self.wins = self.wins + 1<br>
<br>
The instance attribute is not found and the right side falls back to look up<br>
self.wins in the class, i. e. the first time you are effectively running<br>
<br>
self.wins = T.wins + 1<br>
<br>
The left-hand side always denotes an assignment to the instance, so T.wins<br>
will always remain 0.<br>
<br>
It is still good practice to define all attributes that are meant to be<br>
instance attributes in the initialiser:<br>
<br>
class Team:<br>
    def __init__(self, code):<br>
        self.wins = 0<br>
        self.losses = 0<br>
        ...<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="https://mail.python.org/mailman/listinfo/tutor" target="_blank">https://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br></div>