[Tutor] Beginner question on classes

Dave Angel davea at davea.name
Wed Oct 23 12:45:46 CEST 2013


On 23/10/2013 03:25, Corinne Landers wrote:


> --></style></head>
> <body class='hmmessage'><div dir='ltr'>Hi guys, <div><br></div><div>I need a bit of help. </div><div>I'm writing a class, and in the main function I'm trying to say if this method gets called, do this. </div><div>I'm trying things like:</div><div><br></div><div>program = AnimalClass(x,y,z)</div><div>for i in range(x):</div><div>   for j in range(y):</div><div>      for k in range(z):</div><div>      animal = program.animal()</div><div>      if animal: </div><div>          if isinstance(animal,moose):</div><div>          print("There is a moose here")</div><div><br></div><div>It's clearly not correct because it's giving me all sorts of grief, but if anyone knows how to do this correctly I'd very much appreciate it!</div><div><br></div><div>Corrine</div><div><br></div> 		 	   		  </div></body>
> </html>
>

Hi, and welcome to Python, and to the Python tutor list.

Please start by telling us what Python version you're using, and what
OS.

Please post here using a text message, not html, as many email programs
(apparently including yours) cannot correctly convert the html to
text. Your entire program fragment appears on one line in my
newsreader.  And people like me can see the html, but not display it. 
This is a text newsgroup.

Studying other people's responses that do have html abilities, I can see
that you did post an indented code fragment, so I'll copy that here
and comment on it.

> program = AnimalClass(x,y,z)
> for i in range(x):
>     for j in range(y):
>        for k in range(z):
>        animal = program.animal()
>        if animal:
>            if isinstance(animal,moose):
>            print("There is a moose here")

Your problem statement was:  "if this method gets called, do this"

No idea which of these is "this method," nor what you intend by "do
this."  And you needn't test whether it's being called, since presumably
you're calling it.  Usually, you test the return value of a method, but
that's not what you're saying.

Just looking at the code in isolation, I can see that you'll get an
indentation error on the line assigning to animal.  You have to indent
the body of any for statement.

Next, it appears you don't use i, j and k.  So are you intending to just
do the same thing x*y*z times?  If so, then you just should use one
loop, and multiple them together.

Does program.animal() return the same value each time, or is it somehow
supposed to guess the values of i, j, and k ?

Is moose a class name, defined elsewhere that you didn't include?  Then
it ought to be capitalized, to make that obvious.  It's just a
convention, but following conventions will make your code easier to
read.  I know that many classes in the standard lib are not capitalized,
but that's mostly because the names were established many many years
ago, some perhaps before there was the ability to write user classes. 
Pep-8 is the place where such conventions are described.

-- 
DaveA




More information about the Tutor mailing list