[Tutor] Dynamic creation of class instances...

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 22 May 2002 12:03:10 -0700 (PDT)


On Wed, 22 May 2002, Paul Sidorsky wrote:

> alan.gauld@bt.com wrote:
>
> > >  Right now I start out with a list of names, craft a string that
> > >  looks like a class instantiation and then exec the sucker.
>
> > Why does this same question come up every month on this list?
> > It has to be one of the top FAQs for this mailing list.


People with PHP experience appear to take this approach, if:

    http://www.onlamp.com/pub/a/php/2001/05/17/php_foundations.html?page=2
    http://www.phpbuilder.com/columns/robert20000928.php3

is a indication of what an experienced PHP programmer can do with
"variable variables".  Yikes.  So that may be one source of the question.



> Heh I've noticed that too.  Perhaps we need a Tutor FAQ?  I'd volunteer
> to write one except I don't think I've been here long enough yet.

Write one up!  *grin*



> I admit to having tried this as well when I first realized it could be
> done.  I didn't really need it for anything, I just wanted to see if it
> was feasible.  It didn't take too long for me to see that it wasn't, but
> it was fun to try.  I think Python just makes it too easy to do, and
> thus very tempting to try to use in practice.

The Perl folks also allow this approach of "variable variables". Perl's
DBI function "bind_columns()" does this, for example.

But it's less of an issue in Perl because it has a separate namespace for
variables, and I'm assuming PHP does this as well.  That's one of the
reasons every Perl scalar variable has those funny dollar signs.

Python, on the other hand, treats every value --- even functions ---
uniformly as first-class values, so that's why it's not such a hot idea to
do "variable variables" in Python, since they can munge up function names.


I'm off to the airport, so I'll talk to you later!