how to convert string to list or tuple

Steven Bethard steven.bethard at gmail.com
Tue May 31 12:10:11 EDT 2005


Duncan Booth wrote:
> e.g. Assuming that the MyDatabase class does something nasty to a file:
> 
>>>>class MyDatabase(object):
> 
>     def __init__(self, filename):
>         self.filename = filename
>     def initialise(self):
>         print "Splat %s" % self.filename
> 
>>>>eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__()
> 
>         if 'MyDatabase' in `cls` 
>         ][0]('importantfile').initialise()''', dict(__builtins__=None))
> Splat importantfile

Interestingly, I don't seem to be able to create a file object as a 
class attribute in restricted mode:

py> class C(object):
...     def __init__(self):
...         self.f = file('temp.txt', 'w')
...
py> eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__() 
if cls.__name__ == 'C'][0]().f.write("stuff")''', dict(__builtins__=None))
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
   File "<string>", line 0, in ?
AttributeError: 'C' object has no attribute 'f'
py> eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__() 
if cls.__name__ == 'C'][0]().__dict__''', dict(__builtins__=None))
{}

I don't get an error for calling the file constructor, but the f 
attribute is never set AFAICT.

STeVe



More information about the Python-list mailing list