Hi Kirby, I happen to be working through some Python3.0 issues right now. A couple comments:
On Monday 16 March 2009 10:42:24 kirby urner wrote: > > So then it occurs to me, would there by a way to do a kind of special > names overloading such that __next__ (that which triggers the next > cycling to yield) might be replaced with the word kick in some > namespace, as in kick(o) instead of next(o) -- using Python 3.x syntax > here i.e. not o.next(). > > What would a student try, if this were the challenge? > > Simple solution: > >>> def kick(o): > > return next(o) Of couse, the simplest solution is just to bind kick to next: >>> o = iter(range(10)) >>> kick = next >>> kick(o) 0 >>> kick(o) 1 That does the trick. > >>> def f(): # could use a generator expression > > for i in range(10): > yield i > > >>> type(f) # just a function so far > > <class 'function'> > > >>> o = f() > >>> next(o) > > 0 > > >>> kick(o) > > 1 > > >>> kick(o) > > 2 > > >>> type(o) # a generator once instanced > > <class 'generator'> > > Another student might think different and try something like this: > >>> o.__next__ > > <method-wrapper '__next__' of generator object at 0x8367edc> > > >>> o.kick = o.__next__ > > Traceback (most recent call last): > File "<pyshell#51>", line 1, in <module> > o.kick = o.__next__ > AttributeError: 'generator' object has no attribute 'kick' > > "Dang, you can't give your generator object a random attribute i > doesn't already have, write-protected in some way...." Of course, even if this worked, it wouldn't give you the kick(o) behavior that you specified above. I could see where one might try this to get the 2.xish o.kick() behavior. Of course, you could easily get an alias to o's __next__ via simple assignment again: >>> o = iter(range(10)) >>> kicko = o.__next__ >>> kicko() 0 >>> kicko() 1 > Note that the generator type is inappropriate as a base class for your > own kind of user class. > > >>> class newgen (type(o)): > > kick = type(o).__next__ > > > Traceback (most recent call last): > File "<pyshell#10>", line 1, in <module> > class newgen (type(o)): > TypeError: type 'generator' is not an acceptable base type I don't follow the reasoning in this attempt at all. --John _______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig