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

Community
  • 1
  • 1
LB40
  • 12,041
  • 17
  • 72
  • 107
  • What do you mean by "an object named foo of a particular type"? Do you mean, "create an object and bind it to a variable named foo"? That's not going to be very useful, since your other code won't know to access the variable named `foo` to get at the object. Objects don't inherently have names in Python. That is, some objects may have multiple names while others have none. – Blckknght Sep 05 '13 at 08:22
  • I know it's a bit weird but I just have to provide objects to be used by other components built around the same xml files. – LB40 Sep 05 '13 at 08:30
  • @LB. therefore the `object name` and `particular type` part of the question are irrelevant – Bleeding Fingers Sep 05 '13 at 09:17
  • the [edit](http://stackoverflow.com/revisions/18630827/2) should actually be put in the answers section. – Bleeding Fingers Sep 05 '13 at 10:12
  • There is absolutely *no* advantage in doing: `exec('%s = Bunch' % 'foo')` versus `foo = Bunch`. The only thing you do is to slow down the execution of the code. There *could* be an advantage if `foo` was an identifier containing a string created dynamically, but in this case this is a code smell. – Bakuriu Sep 05 '13 at 11:16

1 Answers1

0

You may need something like this:

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


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

foo = Bunch()

XML property value extractor should be implemented as suited.

You might want to have a look at this while we are at names in Python.

Community
  • 1
  • 1
Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
  • not sure to understand but type() returns a type, right ? and what i'd like to do is to define a variable named 'foo'. I'd like to do : foo = Bunch() and foo stems from an XML .... – LB40 Sep 05 '13 at 09:47
  • @LB. You do *not* really want to do that. That's part of your solution, which is almost certainly *wrong*. *Having* to use `exec` is a **huge** code smell. I believe you fell victim of the [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and should really ask a question about how to design your code in order to *avoid* using `exec`, but this requires much more context. – Bakuriu Sep 05 '13 at 11:19
  • @Bakuriu I know it really really smells...the thing is sometimes your client tells you what he wants. The thing is that I have to create a bunch and identify this bunch according to a string. – LB40 Sep 05 '13 at 15:54