[Tutor] Why does the last loop not work?

spir denis.spir at gmail.com
Tue Feb 25 11:49:11 CET 2014


On 02/25/2014 01:59 AM, Gregg Martinson wrote:
> I am trying to generate a list of teams using objects that I collect into a
> list that I can cycle through.  But when I run the last loop there is
> nothing produced.

Look at your last line of code:

      x.print_team

This is just the _name_ of the method print_team, for x. To call it, must add () 
even it take takes no parameter.

A few notes below.

>   I am pretty sure it has to do with my syntax for the
> list and the substitution of a a local variable but I can't figure out how
> to make it work.  Any help would be appreciated.  I am working my way
> though *Learning Python*, but its a long slog.
>
> #!/usr/bin/env python
> def printit (aff,neg):
>      print (aff, "\t",neg,"\tTBD\tTBD")

You don't seem to use this func.

> def header (r):
>      print ("##############################")
>      print ("### Round: ",r,"            ####")
>      print ("##############################")
>      print ("Aff\tNeg\tJudge\tRoom")

May be called 'write-header'?

> class Team(object):
>      code = ""
>
>
>
>

Why so much vertical space?

>      # The class "constructor" - It's actually an initializer
>      def __init__(self,code):
>          self.code = code
>          print ("code is: ",code)
>          self.competitors=[]

Here one blank line may help readability.

>      def print_team(self):
>          print("team code is: ",self.code)
>          print("debated:",end=" ")
>          for x in self.competitors:
>              print (x)
>

Instead of print_team, you should use Python's builtin tools for this purpose. [1]

>      def debated(self,otherTeam):
>          print (x)
>          self.competitors.append(x)

some space here, again?

>      def giveCode(self):
>          return self.code
>
>
> def make_team(code):
>      team = Team(code)
>      return team

This function (a "factory", that creates new objects) is not needed in Python. 
Classes act as factories for their instances, also in syntax. Just as you do above:
     team = Team(code)

> #MAIN Program#
> myTeamCodes=["a","aa","b","bb","c","cc","d"]
> # Make teams
> myTeams=[]#list of teams
> for x in myTeamCodes:
>      myteam=make_team(x)

     myteam = Team(code)

>      myTeams.append(myteam)
> # problem is that myTeams has no value outside of the loop

You assert this, but did you try? Did you add a debug print like:
     print(myTeams)
?

> for x in myTeams:
>      x.print_team

d

[1]
There are two "magic methods" intended for object written output:
* __repr__ is for programmer feedback, when you control, debug, diagnose... (on 
the terminal in general) The best thing in general is to reproduce the original 
notation, as in program source. So, in your case, __repr__ would produce 
"Team(code)".
* __str__ is for user information on the user interface (terminal, GUI, game 
screen...)
The difference with your print_team is that they produce the strings, they don't 
write directly; python magically uses such strings when you ask for an object to 
be written out. Then you can freely use that in print statement directly, or in 
in composite strings.
	# instead of: team.print_team
	print(team)				# eg "Team('c')
	print("team #%d --> %r" %(i, team))	# eg "team #3 --> Team('c')"

__repr__ corresponds to to the code %r, __str__ to %s; whenever they differ, use 
%r for your own feedback in general.


More information about the Tutor mailing list