Re: Formatted string to object
Thankyou everyone for help last time: The following works ok when setting a wx.lib.buttons.GenBitmapTextButton's disabled bitmap in using wxpython instead of this: self.b1.SetBitmapDisabled(self.yellow) i can use this: aaa = 1 result1 = eval(self.b%s.SetBitmapDisabled(self.yellow) % aaa) result YAY! How would i to achieve this with getattr() ? getattr(locals()['parent'], aaa)??? Why would this be better as recommended in previous post here http://groups.google.com.au/group/comp.lang.python/browse_frm/thread/2529515bc85cd954/c2f080b21d668081?q=janamarnum=1#c2f080b21d668081 Any help appreciated ta -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatted string to object
janama wrote: i can use this: aaa = 1 result = eval(self.b%s.SetBitmapDisabled(self.yellow) % aaa) How would i to achieve this with getattr() ? result = getattr(self, 'b%s' % aaa).SetBitmapDisabled(self.yellow) --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Formatted string to object
Hi, can such a thing be done somehow? aaa = self.aaa bbb = %s.%s % ('parent', 'bbb') Can you use strings or %s strings like in the above or aaa = 'string' aaa.%s() % 'upper' Somehow? Thanks for taking a look at this Regards Janama -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatted string to object
Can you use strings or %s strings like in the above or aaa = 'string' aaa.%s() % 'upper' Somehow? Looks like you want to play with the eval() function. aaa = 'hello' result = eval(aaa.%s() % 'upper') result 'HELLO' Works for your second example. May work on your first example too. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatted string to object
Tim Chase wrote: Can you use strings or %s strings like in the above or aaa = 'string' aaa.%s() % 'upper' Somehow? Looks like you want to play with the eval() function. aaa = 'hello' result = eval(aaa.%s() % 'upper') result 'HELLO' Using eval() or exec should be really avoided whenever possible IMHO. This one is best done with getattr(obj, attribute_name [,default]), ie: aaa = string getattr(aaa, 'upper')() 'STRING' -- bruno desthuilliers python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')]) -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatted string to object
janama wrote: can such a thing be done somehow? aaa = self.aaa bbb = %s.%s % ('parent', 'bbb') Can you use strings or %s strings like in the above or aaa = 'string' aaa.%s() % 'upper' Use the getattr() function:: class parent(object): ... class bbb(object): ... pass ... module_ns = __import__(__name__) getattr(getattr(module_ns, 'parent'), 'bbb') class '__main__.bbb' import string getattr(getattr(module_ns, 'string'), 'upper') function upper at 0x00B502B0 I've imported the module itself here so you can use getattr, but you could also use globals() instead of the inner getattr() call:: getattr(globals()['parent'], 'bbb') class '__main__.bbb' getattr(globals()['string'], 'upper') function upper at 0x00B502B0 HTH, STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatted string to object
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 '[EMAIL PROTECTED]'.split('@')]) -- http://mail.python.org/mailman/listinfo/python-list