Re: [Tutor] increment a counter inside generator

2013-03-14 Thread David Knupp

On Thu, 14 Mar 2013, Steven D'Aprano wrote:

If you have some unknown, arbitrary iterable that doesn't support len(),
then you can use the sum() trick:

it = some_unknown_iterable()
sum(1 for x in it)


Yes, of course you are correct. This was my intention, but I chose an 
especially poorly contrived example. Thank you for the clarification.


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


Re: [Tutor] increment a counter inside generator

2013-03-13 Thread David Knupp

On Wed, 13 Mar 2013, Oscar Benjamin wrote:

(it's not actually a generator by the way)


As Oscar points out, you're not working with a generator expression. The 
syntactical difference between a list comprehension and a generator 
expression is subtle. List comprehensions use square brackets, but 
generator expressions use parentheses.



foo = [n for n in xrange(10)]
foo

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

bar = (n for n in xrange(10))
bar

generator object at 0xb7eaadec

FWIW, if you're working with very large lists, but don't need to create 
the full list in memory, then a generator expression is usually preferred. 
To get the number of items a generator would return, you can use sum() 
like this:



gen = (n for n in xrange(some_really_huge_number))
sum(1 for n in gen) # outputs some_really_huge_number


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


Re: [Tutor] Overriding a method in a class

2011-05-13 Thread David Knupp
I think the problem is this bit about overriding an object's internal 
method with one that is defined externally. My gut feeilng is that you'd 
now have to explicitly pass the object (i.e., self) as well as the string, 
i.e.:


B.addstuff(B, WXYZ)

...which seems clunky.

I'm not familiar with this particular technique. Is it common to do 
something like this?



On Fri, 13 May 2011, Terry Carroll wrote:

I have a pretty basic point of confusion that I'm hoping I can have explained 
to me.  I have a class in which I want to override a method, and have my 
method defined externally to the class definition invoked instead. But when I 
do so, my external method is invoked with a different argument signature than 
the method it overrides.


(I'll illustrate with a toy example named toy.py that maintains a list of 
strings; the actual use case is a wxPython drag-and-drop shell that I find I 
keep re-using over and over, so I decided to try to turn it into a 
general-purpose module for my own use.)


### example 1 begin

class Thing(object):
   def __init__(self):
   self.stuff = []
   def addstuff(self, text):
   self.add_the_stuff(text)
   def add_the_stuff(self, s1):
   self.stuff.append(s1)

A = Thing()
A.addstuff(ABCDEFG)
print A.stuff

### example 1 end

So far, this works as expected.  addstuff invokes add_the_stuff; and the line 
print A.stuff prints out as ['ABCDEFG'], as expected.


Now, here's where I am getting befuddled, with the following additional 
lines:


### example, continued
def addlower(self, s2):
   self.stuff.append(s2.lower()) # add it as lower case

B = Thing()
B.add_the_stuff=addlower
B.addstuff(WXYZ)
print B.stuff
### end

My *intent* here is to patch the Thing object named B so that the B's 
add_the_stuff method is replaced with this additional addlower method that I 
define external to the object.  My expectation would be that, just as 
add_the_stuff method was called with two arguments (self and the string), the 
patched-in addlower would also get called the same way.


What I *expect* is to see ['abcdefg'] printed.  What I get is:

Traceback (most recent call last):
 File E:\Personal\py\DragDrop\toy.py, line 22, in module
   B.addstuff(WXYZ)
 File E:\Personal\py\DragDrop\toy.py, line 7, in addstuff
   self.add_the_stuff(text)
TypeError: addlower() takes exactly 2 arguments (1 given)

I'm assuming I'm missing some fundamental concept.  What is it?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



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