Python class __init__(mandatory arguments?)
Erik Max Francis
max at alcyone.com
Mon Jun 16 01:30:32 CEST 2003
Lee John Moore wrote:
> Just for confirmation really: When I create a custom class, is
> it *really* true that def __init__() arguments can only ever be
No, you're doing something wrong. There's nothing special about the
optionality of __init__'s arguments:
>>> class C:
... def __init__(self, x, y): pass
>>> c = C()
> class MyClass:
> param1 = None
> param2 = None
These two are class attributes.
> def __init__(self, param1, param2):
> param1 = self.param1
> param2 = self.param2
These assignments are reversed.
> I'm so accustomed to constructor arguments being mandatory in
> C++ & OP classes....and yes, yes, I realise __init__() is not a
> constructor (even though it looks like one), ...
"Constructor" is a perfectly fine name for Python's __init__; it does a
precisely analogous thing in Python that constructors do in C++ and
Java. Constructors are really initializers, after all.
> ... but if there's a
> way I can *force* arguments upon class instantiation, I'd like
> to know about it. :-)
Maybe if you showed us a precise code example that's exhibiting this
behavior we could help. As it is, even the code sample you show above
doesn't exhibit this behavior.
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Sit loosely in the saddle of life.
\__/ Robert Louis Stevenson
More information about the Python-list