Creating new instances of subclasses.

J. Clifford Dyer jcd at sdf.lonestar.org
Mon Jan 12 14:15:00 EST 2009


On Fri, 2009-01-09 at 10:46 -0800, Dennis Lee Bieber wrote:
> On Wed, 07 Jan 2009 11:38:29 -0500, "J. Cliff Dyer" <jcd at unc.edu>
> declaimed the following in comp.lang.python:
> 
> > I want to be able to create an object of a certain subclass, depending
> > on the argument given to the class constructor.
> > 
> > I have three fields, and one might need to be a StringField, one an
> > IntegerField, and the last a ListField.  But I'd like my class to
> > delegate to the proper subclass automatically, so I can just do:
> > 
> > >>> f1 = Field('abc')
> > >>> f2 = Field('123')
> > >>> f3 = Field('D,E,F')
> 
> 	And how do you differentiate a string that contains a comma from a
> purported list?
> 
> 	What is expected for:
> 
> 	ambiguous = Field('He said "Blast, it won''t work"')

My strings don't look like that.  Nor is that relevant to my question.
I created a simple, uncluttered example to illustrate my issue with
instantiating the proper subclass for a given argument.  I did not try
to make sure my example handled all edge cases properly.

Cheers,
Cliff





More information about the Python-list mailing list