Re: [Tutor] Why does the last loop not work?

2014-02-25 Thread spir

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.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does the last loop not work?

2014-02-25 Thread Alan Gauld

On 25/02/14 00:59, Gregg Martinson wrote:


there is nothing produced.  I am pretty sure it has to do with my syntax
for the list and the substitution of a a local variable


Chris has answered your question but there are a couple
of general points on your code below...


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


Rather than having multiple calls to print you could do the same thing 
with a single call using a triple quoted string. Its largely a matter of 
taste.



class Team(object):
 code = ""


This makes code a class level attribute. I don't think that's
what you want. And you don't seem to use it anywhere.


 # The class "constructor" - It's actually an initializer
 def __init__(self,code):
 self.code = code


This creates an instance attribute which is distinct to your class 
variable above.



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

 def debated(self,otherTeam):
 print (x)
 self.competitors.append(x)
 def giveCode(self):
 return self.code


In Python its idiomatic to not use getter methods like this.
Unless you need to use a method you can just access code
directly.



def make_team(code):
 team = Team(code)
 return team


Unless you u=inrtend to do more magic here this is pretty much redundant.
Calling

spam = make_team(code)

is no better than calling

spam = Team(code)

And it takes more typing, more reading to understand and is slower.


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


I don't know if you've discovered list comprehensions yet but this 
pattern is exactly what they do


somelist = []
for item in anotherList:
   somelist.append(somefunc(item)

So when you see that pattern consider replacing it with:

someList = [someFunc(item) for item in anotherList]

Or in your case:

myTeams = [Team(code) for code in myTeamCodes]

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does the last loop not work?

2014-02-25 Thread Chris “Kwpolska” Warrick
On Tue, Feb 25, 2014 at 1:59 AM, Gregg Martinson
 wrote:
> # problem is that myTeams has no value outside of the loop
> for x in myTeams:
> x.print_team

No, that is not the problem.  The problem is, you’re missing
parentheses there and are not executing the print_team method, just
referring to it.  Do this:

for x in myTeams:
x.print_team()

This causes this output:

[kwpolska@kw-cassandra /tmp]% python foo.py
code is:  a
code is:  aa
code is:  b
code is:  bb
code is:  c
code is:  cc
code is:  d
team code is:  a
debated: team code is:  aa
debated: team code is:  b
debated: team code is:  bb
debated: team code is:  c
debated: team code is:  cc
debated: team code is:  d
debated: [kwpolska@kw-cassandra /tmp]%

Which is kinda wrong.   But that’s the fault of your print_team() and
lack of competitors.
-- 
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Why does the last loop not work?

2014-02-25 Thread Gregg Martinson
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.  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")

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




class Team(object):
code = ""




# The class "constructor" - It's actually an initializer
def __init__(self,code):
self.code = code
print ("code is: ",code)
self.competitors=[]
def print_team(self):
print("team code is: ",self.code)
print("debated:",end=" ")
for x in self.competitors:
print (x)

def debated(self,otherTeam):
print (x)
self.competitors.append(x)
def giveCode(self):
return self.code


def make_team(code):
team = Team(code)
return team

#MAIN Program#
myTeamCodes=["a","aa","b","bb","c","cc","d"]
# Make teams
myTeams=[]#list of teams
for x in myTeamCodes:
myteam=make_team(x)
myTeams.append(myteam)
# problem is that myTeams has no value outside of the loop
for x in myTeams:
x.print_team
--
http://about.me/greggmartinson
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor