Beginner's question - class definition

Tim Peters tim.one at home.com
Mon Jan 15 16:39:00 EST 2001


> Can anyone explain please why the following definition ( from the
> Python documentation)
>
> class Complex:
> 	def _init_(self, realpart, imagpart):
> 		self.r = realpart
> 		self.i = imagpart
>
> ... [doesn't work] ...

A class constructor's name is "__init__", not "_init_".  That is, you're
missing an underscore on each side.  Try this:

>>> class Complex:
	def __init__(self, realpart, imagpart):
		self.r = realpart
		self.i = imagpart

>>> Complex(1,2)
<__main__.Complex instance at 00B3970C>
>>>

easy-mistake-to-make-once-but-hard-to-make-twice-ly y'rs  - tim





More information about the Python-list mailing list