0

I have read many questions about this problem (like this one), suppose I have a string 'foo' and i want to instantiate an object named foo of a particular type. What I would like to do is to be able to dynamically build an object adding properties extracted from an XML file (using the Bunch pattern).

The problem I have is to name the "root" object(the bunch or the container) according to a particular string.

EDIT I've realized that I could do something like

exec('%s = Bunch' % 'foo')

My bad

4

1 回答 1

0

你可能需要这样的东西:

def parseXML(path):
    '''
    @return: {property: value}
    '''
    # ...


def Bunch():
    return type("XML", tuple(), parseXML(PATH))

foo = Bunch()

XML 属性值提取器应该适当地实现。

在我们讨论 Python中的名称时,您可能想看看这个。

于 2013-09-05T09:25:38.730 回答