[Tutor] __init__ for class instantiation?
Gonçalo Rodrigues
op73418@mail.telepac.pt
Fri Apr 25 21:23:01 2003
----- Original Message -----
From: "Zak Arntson" <zak@harlekin-maus.com>
To: <tutor@python.org>
Sent: Friday, April 25, 2003 8:40 PM
Subject: Re: [Tutor] __init__ for class instantiation?
> >>
> >> 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.
> >
> <SNIP!>
> > 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
>
> My best current solution is to do just that. I can do it via __init__, but
> this means the function I want to run once upon the class's definition is
> actually called with every instance of the class. But I'm not going to
> stress about the CPU cycles caused by this :)
>
Just add a class attribute like
_was_get_args_called = 0
and in the __init__ something like
if not self._was_get_args_called:
<do whatever you need>
self._was_get_args_called = 1
But as another poster mentioned what the problem is begging here is really a
metaclass.
HTH,
G. Rodrigues
> --
> Zak Arntson
> www.harlekin-maus.com - Games - Lots of 'em