[Tutor] __init__ for class instantiation?
Fri Apr 25 13:01:01 2003
> On Fri, 25 Apr 2003, Zak Arntson wrote:
>> Here's a short history: I'm working on an interactive fiction engine,
>> and I'd like to create a list of verbs dynamically, based on the
>> classes created.
>> My question is: Is there a function I can define in the base Thing
>> class which would run once Player (or any other Thing child) has been
>> fully defined? Or is my current solution as close as I can get?
> If I understand what you want to do, why not put getActions in the
> __init__ method of Thing? Then, at the end of __init__ for Player (or
> other child classes you define), call Thing's constructor:
> class Thing:
> def __init(self):
> ... code ....
> class Player(Thing):
> def __init__(self):
> ... code ...
The problem here (as with __new__) is that __init__ will be called with
every creation of a Player instance. I just want to getActions once the
Player class is defined. Otherwise, I run getActions() _every_ time I
create a Player (or any other Thing child). This wouldn't hurt much,
considering even with multiplayer, a new player wouldn't be created often.
It's just not as elegant as running only when Player is defined.
But! Your method is cleaner than my approach. I'll put getActions into
Thing's __init__. It seems more elegant than me plonking a getActions
after every class definition.
That leads me to a side question, though: Does __init__ of a parent only
run when explicitly called by the child's __init__?
www.harlekin-maus.com - Games - Lots of 'em