Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52534

Re: many constructors in a class?

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: many constructors in a class?
Date 2013-08-14 18:40 -0400
Organization IISS Elusive Unicorn
References <520b913f$0$13995$426a74cc@news.free.fr>
Newsgroups comp.lang.python
Message-ID <mailman.589.1376520059.1251.python-list@python.org> (permalink)

Show all headers | View raw


On 14 Aug 2013 14:16:31 GMT, climb65 <climb65@laposte.net> declaimed the
following:

>Hello,
>
>here is a small basic question :
>
>Is it possible to have more than one constructor (__init__ function) in a 
>class? For instance, to create an object with 2 different ways? If my 
>memory is good, I think that with C++ it is possible.
>
	Well... Secret Python-Fu -- __init__() is NOT a constructor. The
constructor is __new__(); __init__() is just an initializer of whatever
__new__() creates.


	Ignoring that detail... NO!

	If the goal is to be able to "leave out" some parameters, that is
performed by setting default values for the missing parameters.

	def __init__(self, these, are, mandatory, but=None, this=None, 
								is=None, optional=None):
		self.these = these
		self.are = are
		self.mandatory = mandatory
		if but is None:
			self.but = these + are
		else:
			self.but = but
	#etc.

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

many constructors in a class? climb65 <climb65@laposte.net> - 2013-08-14 14:16 +0000
  Re: many constructors in a class? Phil Le Bienheureux <phil.le.bienheureux@gmail.com> - 2013-08-14 16:46 +0200
  Re: many constructors in a class? duncan smith <buzzard@invalid.invalid> - 2013-08-14 16:07 +0100
  Re: many constructors in a class? Beth McNany <beth.mcnany@gmail.com> - 2013-08-14 10:40 -0400
  Re: many constructors in a class? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-14 18:40 -0400
  Re: many constructors in a class? Steven D'Aprano <steve@pearwood.info> - 2013-08-15 07:23 +0000
    Re: many constructors in a class? Fábio Santos <fabiosantosart@gmail.com> - 2013-08-15 11:09 +0100
    Re: many constructors in a class? Roy Smith <roy@panix.com> - 2013-08-15 09:00 -0400

csiph-web