Checking for required arguments when instantiating class.

Piet van Oostrum piet at cs.uu.nl
Thu May 7 09:04:57 EDT 2009


>>>>> Chris Rebert <clp2 at rebertia.com> (CR) wrote:

>CR> On Wed, May 6, 2009 at 5:24 AM, Piet van Oostrum <piet at cs.uu.nl> wrote:
>>>>>>>> Lacrima <Lacrima.Maxim at gmail.com> (L) wrote:

>L> But what if I have to instantiate any class with 3 or 4 required
>L> arguments? How can I do it?
>>> 
>>> cls.__init__.im_func.__code__.co_argcount
>>> 
>>> This will include self, so it will be 1 in First and 2 in Second.

>CR> AFAICT, that would count non-required arguments too, which isn't
>CR> strictly what the OP requested.

If you mean, parameters with default values, yes. Of course you can find
out how many of these there are from cls.__init__.func_defaults, but
this will get dirtier and dirtier.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list