2001 Enchancement Wishlist

Pearu Peterson pearu at cens.ioc.ee
Thu Jan 4 05:47:41 EST 2001


On Wed, 3 Jan 2001, Alex Martelli wrote:

> "Raymond Hettinger" <othello at javanet.com> wrote in message
> news:3A4ECDA9.E2796F78 at javanet.com...
>     [snip]
> > I was hoping to avoid a factory method or global variable or using
> > modules to create single instances.  Compared to enriching __init__,
> > these approaches seem kludgy,
> 
> Why would a factory function be "kludgy"?  It's one of the most
> classic and useful design-patterns, and I've as yet seen NO reason
> at all, from either you or Pearu, showing this pattern as in any
> way undesirable.

For the record, I use now

  class Real:
      def __init__(self,num):
          self.num = num
      <other number class methods>

  class Complex:
      def __init__(self,real,imag):
          self.real,self.imag = real,imag
      <other number class methods> 

  _Complex = Complex

  def Complex(real,imag):
      if imag == 0:
          return Real(real)
      return _Complex(real,imag)

for modeling

  class Complex:
      def __init__(self,real,imag):
          if imag == 0:
              return Real(real)
          self.real,self.imag = real,imag
      <other number class methods> 

And the idea works fine for my application. Though, any comments are
appreciated.

PS: Here Complex and Real classes are for illustration only.

Thanks,
	Pearu




More information about the Python-list mailing list