I'm trying to define a class dynamically and then instantiate it . For this what i'm doing is , i have the class definition as a string and i'm feeding it to the eval function to define it during runtime .
f="""class f:
def pr(self):
print "asdfg"
"""
>>> eval(f)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
eval(f)
File "<string>", line 1
class f:
^
SyntaxError: invalid syntax
Why im doing this is because there are lots of classes that can be used but rarely are they all needed together. Plus i allow user's to add new classes to the application.
Thanks in advance .
Edit -------------------
This is python 2.5 output
data = """class f:
def pr(self):
print "asdfg"
"""
>>> exec(data)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
exec(data)
File "<string>", line 4
^
SyntaxError: invalid syntax
#What i think the problem is "\n"
>>> data
'class f:\n def pr(self):\n print "asdfg"\n '
im not sure, but that is where the error is pointing.
the reason for using python 2.5 is that , scapy is a part of the program and it has support for only python 2.5.
fwould be filled with the content of a Python source file?dataso that the closing triple quotes"""appear on the end of the third line (so likeprint "asdfg" """? I don't really know why this is happening, though.