0
line1 = "class A:\n\tpass"
line2 = "class B:\n\tpass" 
line3 = "class C:\n\tpass"
line4 = "print(issubclass(C,A) and issubclass(C,B))"

exec(line1...4) results in "False" printed to the console, as it should be.

However, if I then:

myList = [str(exec(line4))]
print(myList)

The string in myList is "None", where I'd like it to be "False" as printed on the console.

I've tried aVariable = exec(line4) but this also prints "None".

Any help welcome.

I'm new to Python and I feel like there's a nuance I'm missing.

Thanks in advance!

Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
justajolt
  • 141
  • 11
  • 2
    Executing a line is not the same as **evaluating** it. There is an [`eval`](https://docs.python.org/3/library/functions.html#eval) function (which you should also be careful about using in real code). – khelwood Nov 25 '19 at 16:07
  • 1
    If you are new to Python, you should absolutely *not* be using `exec` or `eval`. – chepner Nov 26 '19 at 18:40
  • Upvoted you, @chepner because it's true for real code, but the best way to learn how something works is to play with it extensively. – justajolt Nov 27 '19 at 10:51
  • `exec` and `eval` are far, *far* down the list of things you need to know right now. – chepner Nov 27 '19 at 13:51
  • Oh, I'm sure I don't _need_ to know them, but the best time to start learning about anything, in any field, is the moment in which interest is piqued. Whilst `exec` and `eval` are certainly a diversion from my current curriculum, they're certainly also being a welcome and informative one. If you're offering to help me achieve my learning goals, I'd certainly welcome your input, @chepner, if your reputation is anything to go by. PM if you'd like a tutee and we can have a chat. – justajolt Nov 27 '19 at 15:15

2 Answers2

3

printing is not the same as returning. As per the documentation, exec always returns None:

[...] The return value is None.

Using something like eval won't help you either, because your line evaluates to None as well (I repeat: printing is not the same as returning)

If you want to actually capture what's being printed, then take a look at this other SO question


If what you want is to actually return instead of print, then change your line accordingly:

line4 = "issubclass(C,A) and issubclass(C,B)"

And then, use eval:

myValue = eval(line4)
# myValue = False
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
-1

You want eval instead of exec, as to why you are doing this I can not understand it but will not ask: (Also your line4 will never evaluate to a boolean, as it is a print which also returns none):

line4 = 'myList = [str(issubclass(C,A) and issubclass(C,B))]'

exec the first lines to create your classes, then eval line4: FOR SOME REASON LIST COMPREHENSION DOES NOT ASSIGN, SO:

exec(line1)
exec(line2)
exec(line3)
exec(line4)

myList                                                                                                                                                                                           

Out[44]: ['False']

E.Serra
  • 1,495
  • 11
  • 14