[Tutor] How to make comparison work

Alan Gauld alan.gauld at btinternet.com
Fri Apr 11 10:20:15 CEST 2014

On 11/04/14 00:27, Gregg Martinson wrote:
> I have been working through a fairly simple process to teach myself
> python and I am running into a problem with a comparison.  Can anyone
> tell me where I am going wrong?
> #!/usr/bin/env python
> class Team(object):
>      code = ""
>      opponents_debated=[]
>      wins=0
>      losses=0
>      competitors=[]

All of these variables are class variables rather than instance 
variables. That means they are *shared* by all instances.

>      def __init__(self, code):
>          self.code = code
>          self.competitors.append(code)
>          #self.school_teams.append(code)

So when you run init() you are changing the values for all your teams.
I strongly suspect you want those variables inside init so that each 
object has its own value?

>      def havedebated(self, otherTeam):
>          print (self.code, "compares ", otherTeam, "is in",self.competitors)
>          if otherTeam in self.competitors:
>              return 1
>          else:
>              return 0

When you do the comparison you are checking against the shared 
collection which will, I think, have all the teams in it, so it will 
always be true.

I haven't studied that in detail but that's what a quick
glance suggests to me.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list