Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Name and File Name

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

like image 416
Sumon Tagoe Avatar asked Jul 09 '11 22:07

Sumon Tagoe


People also ask

Why should filename and class name be same?

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.

Does class name have to match file name Java?

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 ".

Can class name and filename be different in C#?

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.

Can you save a Java source file by name than class name?

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.


1 Answers

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.

like image 56
hammar Avatar answered Sep 22 '22 14:09

hammar