[Python-ideas] Make-statement [Re: Different interface for namedtuple?]
arnodel at gmail.com
Sun Mar 6 01:56:03 CET 2011
On 5 Mar 2011, at 23:18, Greg Ewing wrote:
> Carl M. Johnson wrote:
>> make NamedTuple(rename=True) Point:
>> x, y
>> make Table() Author:
>> firstname, lastname, DOB = Str(), Str(), Date()
> I did some thinking about this kind of thing once when I was
> considering Python as a language for interactive fiction. When
> writing IF, you tend to have a lot of unique objects with
> special behaviour, and to support this, dedicated IF languages
> such as TADS usually have a construct that's somewhere between
> a class declaration and an instantiation.
> For Python, I postulated an "instance" statement that would
> be used something like this:
> instance Wardrobe(Thing):
> name = "wardrobe"
> description = "A nice mahogany double-door wardrobe."
> def take(self):
> print "The wardrobe is too heavy to pick up."
> What this would do is first create an anonymous subclass of
> Thing, and then instantiate that subclass.
That's interesting, I once wrote a simple IF engine in Python and I wanted to achieve this almost exactly! I solved it as follows: instances are declared as classes inheriting from their type and the special type named Instance. So the the class definition below binds "House" to an instance of Location.
class House(Instance, Location):
description = "a small country house"
long_description = "You are in a small country house."
objects = RedKey, WoodenChest, BackDoor
message('you step through the door into the garden.')
location = Garden
message("You can't go THROUGH the door, it's closed!")
Instance was defined as follows:
def __init__(cls, name, bases, attrs):
__metaclass__ = MetaInstance
def MetaInstance__new__(meta, name, bases, attrs):
bases = list(bases)
cls = bases.pop()
MetaInstance.__new__ = MetaInstance__new__
More information about the Python-ideas