class / instance question
belinda thom
bthom at cs.hmc.edu
Thu Mar 22 22:17:41 EDT 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
>> constructor
>>
>> 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()
> game.start_play()
Aha....I was trying things like:
game_class.__init__(...)
which complained then about self.
Figured there had to be a way. Many thanks---you've saved me some time.
--b
More information about the Python-list
mailing list