[Tutor] __init__ for class instantiation?

Alan Gauld alan.gauld@blueyonder.co.uk
Fri Apr 25 14:10:02 2003


> def getActions (thingClass): ...
>
> class Player (Thing):
>   def __init__ (self):
>      ... code ...
>   def act_go (self, parsed):
>      ... code ...
>   def act_look (self, parsed):
>      ... code ...
>
> 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?

The short answer is no I don't believe so, not as such.
The definition of a class doesn't (except at a very deep secret
level) trigger any action in Python so you can't force it to
do anything.

You could maybe play some tricks with class constructor functions
that return a Thing class after running your magic method.

class Thing:
    def doTheBiz():   # use new static method syntax here
 ... define rest of Thing here...

def makeThing():
    t = Thing  # note t is a class not an instance!
    t.doTheBiz() # needs to be a class (ie static) method
    return t

Then in the new class defintions call makeThing:

class C(makeThing()):
   # define C here

But I haven't tried this....

Normally you wouyld do it at the instance level, is there any reason
why you can't do that? ie. Wait till the class is instantiated...

Alan G