When defining a class in Python, why does the class usage need to be in a new line? This becomes a bit annoying when trying to run python programs from a string (python -c '....'
) because I can't define and use the class on the same line. Is there a way to achieve this?
For example, the following fails:
$ cat class_play.py
class ClassA(): pass; a = 3; print(a); print(ClassA)
$ class ClassA(): pass; a = 3; print(a); print(ClassA)
$ python class_play.py
3
Traceback (most recent call last):
File "class_play.py", line 1, in <module>
class ClassA(): pass; a = 3; print(a); print(ClassA)
File "class_play.py", line 1, in ClassA
class ClassA(): pass; a = 3; print(a); print(ClassA)
NameError: name 'ClassA' is not defined
but using the class in a new line works:
$ cat class_play.py
class ClassA(): pass; a = 3; print(a);
print(ClassA)
$ python class_play.py
3
<class '__main__.ClassA'>