[Tutor] _init_() arguments
Stu Rocksan
132zed at gmail.com
Fri Sep 9 12:10:59 CEST 2011
I have a very basic question that I am sure has a simple answer. I
would be very appreciative of anyone that would set me straight.
Python 2.7.2
I am simply trying to pass arguments. Based on the documentation that
I've read so far _init_() is called upon instance creation and the
arguments are those passed to the class constructor expression. That
all makes sense to me but the problem is when I try to actually pass
the argument. I get an TypeError that states "This constructor takes
no arguments."
Even using code direct from the tutorials included in the
documentation gives me the same error. I doubt that there would be
bad code coming direct from the official website...right? So the
problem must be me.
Example:
class Complex:
def _init_(self, realpart, imagpart)
self.r = realpart
self.i = imagpart
x = Complex(3.0, -4.5)
x.r, x.i
# theoretical output
(3.0, -4.5)
This is taken direct from the tutorial included in the documentation
dated September 8, 2011. If you try to run this you will get a
TypeError: this constructor takes no arguments.
It even says in the explanation before this code:
Of course, the __init__() method may have arguments for greater
flexibility. In that case, arguments given to the class instantiation
operator are passed on to __init__().
I've tried similar code from other beginner Python books and I get the
exact same result.
I am sure that I am missing something simple. Thanks in advance
More information about the Tutor
mailing list