class / instance question
bthom at cs.hmc.edu
Fri Mar 23 03:17:41 CET 2007
On Mar 22, 2007, at 7:10 PM, Ben Finney wrote:
>> abstract base class, where I've used an "abstract()" hack to
>> somewhat enforce this).
> You can use the 'NotImplemented' built-in object, or the
> 'NotImplementedError' exception, for this purpose:
Thanks---I'd forgotten about that.
>> I now want to be able to pass either of these classes into a game-
>> playing engine, for instance a function
>> playGame(classOfGameToBePlayed) :
>> # inside of which I create a new game using that class's
>> Is this possible, and if so, how to approach it?
> It works just as you describe. The class is an object, that can be
> passed as an argument just like any other object. That object is
> callable, and invokes the class constructor to return a new instance
> of the class.
> def play_game(game_class):
> """ Play a new game """
> game = game_class()
Aha....I was trying things like:
which complained then about self.
Figured there had to be a way. Many thanks---you've saved me some time.
More information about the Python-list