Formatted string to object

bruno at modulix onurb at xiludom.gro
Mon Jun 19 12:59:48 EDT 2006


janama wrote:
> Hi,
> 
> can such a thing be done somehow?
> 
> 
> aaa = self.aaa
> bbb = %s.%s % ('parent', 'bbb')

Given the first line, I assume this is inside a method body, and parent
is a local var. Then the answer is:

bbb = getattr(locals()['parent'], 'bbb')

read the doc for these two functions to get more infos.

> Can you use strings or %s strings like in the above or
> 
> aaa = 'string'
> aaa.%s() % 'upper'
> 
> Somehow?

Not directly - but here again, getattr() is your friend.


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list