[Tutor] Class instantiation parameters

Alan G alan.gauld at freenet.co.uk
Sun Aug 7 19:15:07 CEST 2005

> page = Show(type=type, id=id)
> class Show:
>     def __init__(self, type, id):
>         self.id = id
> ...
>        return self

First a couple of comments.
You don't need to return self from __init__.
You can only instantiate Show *after* you've defined it.

> For two parameters, this is relatively simple. 
> But if I have for example 10 parameters on instantiation, 

10 *parameters* in the constructor is not that unusual. 
But at instantiation you would not normally expect to pass 
more than 4-6 *arguments*. The others would normally use 
default values.

**args is indeed the solution to this so you may have 
figured it all out already, but your terminology is
slightly confusing (because while it is strictly valid 
in what it says, but so unlikely in its meaning, that 
I suspect you are getting some of your terms mixed up?)

> assigning each value the the class object manually 
> will be really clumsy.

It's actually how most OOP languages do it! Perl is unusual 
baecause of its shortcuts and Python provides a half-way house


Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list