Grokbase Groups Python tutor May 2004
FAQ
I am parsing an XML file, and based on certain tags, i want to create
certain objects. During the parsing i generate a string, with which i
determine what type of object I want to create. The simple way is to
have som if statements to determine it, but I was wondering if there is
not a more "smart" method. I also do lot of PHP programming, where it is
possible to do:

<?php
$object = "ClassToCreate";

$newObject = new $object();

?>

In that way I am able to with very few lines, to create an object based
on a string. Is there any way of doing something similar in Python?

regards

Martin Hjort Eriksen

Search Discussions

  • Roeland Rengelink at May 24, 2004 at 5:07 am

    Martin Hjort Eriksen wrote:

    I am parsing an XML file, and based on certain tags, i want to create
    certain objects. During the parsing i generate a string, with which i
    determine what type of object I want to create. The simple way is to
    have som if statements to determine it, but I was wondering if there
    is not a more "smart" method. I also do lot of PHP programming, where
    it is possible to do:

    <?php
    $object = "ClassToCreate";

    $newObject = new $object();

    ?>

    In that way I am able to with very few lines, to create an object
    based on a string. Is there any way of doing something similar in Python?
    The canonical way to do this with Python is to create a dictionary that
    maps strings to classes. E.g:

    class A:
    ...

    class B:
    ...

    cls_map = {'A': A, 'B':B}

    def create_class(a_string):
    return cls_map[a_string]()

    If your strings match the class names then you could use eval()

    instance = eval(a_string+"()")

    Hope this helps,

    Roeland
  • Martin Hjort Eriksen at May 24, 2004 at 5:26 am

    The canonical way to do this with Python is to create a dictionary
    that maps strings to classes. E.g:

    class A:
    ...

    class B:
    ...

    cls_map = {'A': A, 'B':B}

    def create_class(a_string):
    return cls_map[a_string]()

    If your strings match the class names then you could use eval()

    instance = eval(a_string+"()")

    Hope this helps,

    Roeland
    This helped...thank you very much...

    A second question in the same area, again the start point is PHP, where
    you have variable variables. I have also looked through the Python
    litterature, and cannot find anything that helps.

    The problem is, as I instantiate the objects, I will put them in an
    dictionary, where I have 4 dictionaries, one for each type of class.
    Since I have mulitple instances if the classes, I have added copy to the
    above code that Roeland sent. Based on that string I want to insert it
    into the correct dictionary, without using if many if sentences. PHP
    example again:

    <?php
    $foo = array();
    $type = "foo";
    $objectToInsert = new bar();

    $$type[] = $objectToInsert;

    ?>
  • Riccardo Lemmi at May 24, 2004 at 10:26 am

    Martin Hjort Eriksen wrote:

    I am parsing an XML file, and based on certain tags, i want to create
    certain objects. During the parsing i generate a string, with which i
    determine what type of object I want to create. The simple way is to
    have som if statements to determine it, but I was wondering if there is
    not a more "smart" method. I also do lot of PHP programming, where it is
    possible to do:

    <?php
    $object = "ClassToCreate";

    $newObject = new $object();

    ?>

    In that way I am able to with very few lines, to create an object based
    on a string. Is there any way of doing something similar in Python?

    regards

    Martin Hjort Eriksen
    From shell:
    class ClassToCreate:
    ... pass
    ...
    newObject = globals()['ClassToCreate']()
    newObject
    <__main__.ClassToCreate instance at 0x4029e98c>
    >>>

    If you use a module, for example:
    import UserDict
    ClassToCreate = getattr(UserDict,'UserDict')
    x=ClassToCreate()
    x
    {}
    >>>
    --

    Riccardo Lemmi
  • Alan Gauld at May 24, 2004 at 1:00 pm

    I am parsing an XML file, and based on certain tags, i want to create
    certain objects.
    Sounds exactly like SAX to me.
    I assume you are using the standard parsers and not trying to
    craft something by habd based on regexs or somesuch?

    SAX is an event based parser so you simply create a function
    that you associate with each XML tag you are interested in.
    When the parser hits that tag type it calls your function.
    <?php
    $object = "ClassToCreate";

    $newObject = new $object();
    You could do something like that with eval() but using eval()
    and exec() should really be a last resort, better to use the
    power of the tools IMHO.

    Alan G.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouptutor @
categoriespython
postedMay 24, '04 at 4:43a
activeMay 24, '04 at 1:00p
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase