[Tutor] Unexpected iterator

2009-11-12 Thread Jeff R. Allen
Hello,

I am working my way through the tutorial, and I like trying
variations, just to see what expected errors look like, and other ways
things could be written.

I tried a, b = 0, 0 and that worked.

Then I tried this to (maybe) set both a and b to 0:

>>> a, b = 0
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'int' object is not iterable

I understand why it doesn't work, but I don't understand the wording
of the exception. Could someone explain how I accidentally introduced
iteration into the picture with my syntax? I have a feeling there's an
interesting lesson hidden in this example...

Thanks.

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


Re: [Tutor] How to call a method with a print statement?

2009-11-12 Thread Jeff R. Allen
You are looking for the __str__ method. See
http://docs.python.org/reference/datamodel.html#object.__str__

class Foo():
  def __init__(self):
     pass

  def  __str__(self)
     return "hello world!"

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


Re: [Tutor] Unexpected Result in Test Sequence

2009-11-16 Thread Jeff R. Allen
When you declare list1 before "def p()" you are making it global. That
means it will keep its values between invocations of p(). When you
start function p, you don't reset list1 to empty. You divide each time
by 1000, and but your list1 list is growing and growing and growing.
That's why the total is growing, not giving the answer you were
expecting.

Either you need to reset list1 to empty each time p() starts, or (if
you want to accumulate the results between calls to p()) you need to
divide by the true number of items in the list, not by 1000. So
replace /1000 with /len(list1).

However, that brings me to something else interesting. You are doing
an integer divide. That means you'll get integer results, which for
your purposes are much less interesting than floating point ones.
Replace the /1000 with /1000.0 and see what you get. (Move the
declaration of list1 into p() also.)

Another thing... play in the interpreter with range(1,5) and
range(0,5). Can you see another little bug your program has? It is not
doing precisely what you think it is yet...

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


Re: [Tutor] Do you use unit testing?

2009-11-16 Thread Jeff R. Allen
> As a discipline - work out what we want to test, write the test, watch
> it fail, make it pass - I find this a very productive way to think and
> work.

There's an emotional aspect too. Keeping a positive aspect keeps up
flow, and seeing tests go from failing to passing feels really good.
Making a change you're not completely confident in and seeing tests
start failing immediately is also quite nice .. you immediately feel
how close to correct the change in question is, and if it's worth
fixing up or is leading you the wrong direction and should be reverted
allowing you to take another stab at it.

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