0

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.

4
  • 3
    If users of your program need to create new classes, you're doing something wrong. Commented Jan 11, 2014 at 3:29
  • 1
    Are you trying to desing some sort of plug-in system, where f would be filled with the content of a Python source file? Commented Jan 11, 2014 at 3:47
  • @ixe013 : yup , i think the below given solution will work. i actually forgot about it " exec() ". Commented Jan 11, 2014 at 4:38
  • 1
    @thecreator232 Hm. Maybe try altering data so that the closing triple quotes """ appear on the end of the third line (so like print "asdfg" """? I don't really know why this is happening, though. Commented Jan 11, 2014 at 5:12

1 Answer 1

4

Use exec, not eval. exec will execute arbitrary code, while eval will only evaluate expressions (e.g. eval('2+3') == 5).

>>> data = """class f:
    def pr(self):
        print "asdfg" """
>>> exec data
>>> instance = f()
>>> instance.pr()
asdfg

There is probably a better way to achieve what you want to do, though. Having users of your program create new classes seems like a suboptimal design choice.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanx ! i forgot about it "exec()".
i do know that it isn't the best way to do it, but its the only way i currently know . Pls share what are the other ways of doing it .
does the above given method work in Python 2.5 also ?
@thecreator232 I believe that the functionality of exec is essentially unchanged from Python 2.4 forward (so I think it's the same in Python 2.5), but I don't have an install of Python 2.5 available to test this.
@thecreator232 Please edit that into your question so that it can be properly formatted as code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.