> My schtick involves a mythical Python5, an add-on to my current courses, > wherein we adopt a Model-View-Controller approach, as start with really > simple views (i.e. ASCII art), knowing in advance the visualizations will > be upgraded, using both render-time and real-time pipelines. The Model is > of "tractors in a field", the controller being the user via interactive > Python i.e. a very generic idea of MVC). > > What's important about the Tractor class is it defines an iterator, i.e. has a __next__ as well as __iter__ method.
In one implementation, the entire Tractor is a generator function, not a class, based around a yield that takes new data through tractor.send( ). The yield / send syntax is of growing importance in Python, as iterators come into their own. Here's a simple demo of the yield / send concept: from string import ascii_lowercase as lowercase, digits from random import choice def tumbler(alpha=False): while True: if alpha: yield choice(lowercase) else: yield choice(digits) print("With reinitialization...") roller = tumbler() # numeric roller print(next(roller)) print(next(roller)) roller = tumbler(True) # alpha roller print(next(roller)) print(next(roller)) def tumbler(alpha=False): """ self.send() pipes to yield's recipient """ d = {} while True: if alpha: d = yield choice(lowercase) else: d = yield choice(digits) if d: alpha = d['alpha'] print("With send") roller = tumbler() # numeric roller print(next(roller)) print(next(roller)) roller.send({'alpha':True}) # switch! print(next(roller)) print(next(roller)) In the 2nd version of tumbler, there's a way to switch from numeric to alpha tumbler (and back) by sending in a dict. In the 1st version, you just have to start another tumbler, initialized differently. The default tractor, unlike the default turtle, has some sense of mortality i.e. unless you keep feeding it, it becomes inoperable. Of course it would be easy to subclass a turtle in Pythons exiting turtle library to have such an attribute. In Logo, a turtle is not "literally" a turtle and you can easily imagine changing the icon, to a tractor for instance. In the "tractor art" version, a way of teaching both Python and computer related concepts, there is more emphasis on taking "tractor" seriously, as a way of extending the metaphor outward, to the surrounding field and farm. More of a namespace is being claimed. Generality is lost, but this is not "a language" so much as a "theme" for inter-connecting lesson plans. A "tumbler" is not a tractor, more like a tumble weed, but your basic tractor is like a Turing device in that it may read and/or write a character in its current "cell". This is how a tractor "plows" its field, it changes the letters it goes over, as it goes over them (a kind of "tilling"). As mentioned earlier, things start in ASCII but given the MVC overview, when know the visualizations won't need to stay that primitive. Kirby
_______________________________________________ Edu-sig mailing list Edu-sig@python.org https://mail.python.org/mailman/listinfo/edu-sig