Python class __init__(mandatory arguments?)
Tom Hanks
thanks200 at hotmail.com
Mon Jun 16 00:02:24 EDT 2003
Lee John Moore <leej at dsl.pipex.com> wrote in message news:<bcitu2$jrrv8$1 at ID-129053.news.dfncis.de>...
> Just for confirmation really: When I create a custom class, is
> it *really* true that def __init__() arguments can only ever be
> optional? Eg:
Mr Moore,
No. In my experience __init__ arguments without default values
(or using the *args, **kwargs idiom) are required and not optional,
exactly like all other functions.
>
> class MyClass:
> param1 = None
> param2 = None
> def __init__(self, param1, param2):
> param1 = self.param1
> param2 = self.param2
>
> myinstance1 = MyClass()
> myinstance2 = MyClass("Hello", "World")
> [snip]
>
> I'm really unhappy that myinstance1 is allowed. ;-)
I would be unhappy too. :-(
Luckily running your code under 2.2.1 I get a traceback :-)
Traceback (most recent call last):
...
TypeError: __init__() takes exactly 3 arguments (1 given)
I hope this info leaves you less confused than when you started...
Tom.
More information about the Python-list
mailing list