I am from Java background. I am going through the official Python tutorials but can't seem to find the information in relation to Python source file names and classes.
In Java, file name is the same as main class name plus the .java extension. In Python what's the case? In the examples of official tutorials, they are writing multiple classes and there's no mention of the file name. I am kind of lost.
I have a file name called test_pie.py. The content is-
class ListTest:
list1 = [2, 'a', 'ab', 'c', 'aa', 0]
list2 = ['b', list1[-2:-5]]
def PrintList(self):
print list1
print list2
For list1 and list2: I get-
Undefined variable: list1 list Found at: test_pie
Undefined variable: list2 list Found at: test_pie
The main reason for the class and file name to be same are to make the job of the complier easy to check which class it needs to run, in the whole list of the Java classes. So it's a good practice to have filename and class name as same.
In Java, the java file name should be always the same as a public class name. While writing a java program first it is saved as a ". java" file, when it is compiled it forms byte code which is a ".
No, in C# you don't have to name your class the same as your file. Along a similar line, you can have multiple public classes in on file.
Yes,it is possible to compile a java source file with different file name but you need to make sure none of the classes defined inside are public... when you compile the source file the corresponding . class files for the classes inside the source file are created.
In Python a single file constitutes a module, which is similar to a namespace in Java, so you would have all the classes for a single namespace in the same file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With