[Tutor] Re: Not sure how to phrase this question

Emile van Sebille emile@fenx.com
Mon, 16 Sep 2002 19:58:48 -0700


Arthur:
> Something that I guess can be called a simple class factory.  I have a
> routine that will analyze *args sufficiently to provide information as
to
> which class to be called from the ClassFactory to which *args is
> passed.
>
> In the end I want to create an instance of the called/returned class,
not of
> the ClassFactory class.

I don't think you need __new__, or if you do, you'll want to read up on
the existing documentation and prior discussions.  Oh... I couldn't find
the docs.  There are some usage examples in the demo/newmetaclasses, but
they only seem to exist in cvs.  All this probably has a bit to do with
the lack of response you mention.

Anyway, it sounds like you want to create an interface to, eg, allow
students to experiment with geometric properties.  To support this, you
want to create class instances that provide a flexible interface and do
not require arguments passed in an expected sequence.  ISTM a wrapper
class could do this trick.

class _Intersection:
    def __init__(self, plane=None, line=None):
        self.line = line
        self.plane = plane
class Plane:
    def __init__(self):
        self.ima = 'plane'
class Line:
    def __init__(self):
        self.ima = 'line'
class Intersection(_Intersection):
    def __init__(self, *args, **kwargs):
        for ii in args:  kwargs[ii.ima] = ii
        _Intersection.__init__(self, **kwargs)

#test
l = Line()
p = Plane()
ii = Intersection(l,p)
jj = Intersection(p,l)

print ii.line
print jj.plane

HTH, and if you keep trying metaclass stuff, keep us up on your progress
and findings.  ;-)

--

Emile van Sebille
emile@fenx.com

---------