Call it remotely cool_func (): Super Cool! Put Most Code Into a Function or Class. The Python program (pt3.py) was executed directly (as opposed to being imported from another program), and we see the special global variable __name__ has the value __main__. Dieser Online-Kurs ist so aufgebaut, dass man prinzipiell Python auch alleine lernen kann. print type(__name__) print __name__. Old-Style Classes . To fully understand what __name__ is and how it used, let’s go through a series of examples. Call other functions from main(). Python comes with a number of special variables and methods whose name is preceeded and followed by __. In fact, because of this, it's recommended that you don't write your own variables using this pattern as to not confuse the Python interpreter. Create a function called main() to contain the code you want to run.
A special variable called __name__ provides the functionality of the main function. In the Python realm, a class can be one of two varieties. Let’s create a simple Python script in a file called test.py # test.py print("__name__ in test.py is set to " + __name__) What this line does is that it is going to print out the variable __name__ on the screen so that we can examine it. Python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Instance Attributes Kurse und Schulungen. Use __name__ to control execution of your code. This chapter is also available in our English Python tutorial: Class vs. Objects can contain arbitrary amounts and kinds of data. Example 1. __name__ and __main__ __name__ defines the namespace that a Python module is running in. >> python other.py __name__: cool Call it locally cool_func (): Super Cool! Running the above code gives us the following result − … With old-style classes, class and type are not quite the same thing. Put most code into a function or class. No official terminology has been decided on, so they are informally referred to as old-style and new-style classes. This will call the function main() and when main finishes, it will exit giving the system the return code that is … As it is an in-built variable in python language, we can write a program just to see the value of this variable as below. Remember that the Python interpreter executes all the code in a module when it imports the module. Jedes Python Modul hat einen im Attribut __name__ definierten Namen, und wenn dieser Name '__main__' lautet, dann bedeutet dies, dass das Modul als eigenständiges Programm vom Benutzer ausgeführt wurde, und wir können dementsprechende Aktionen durchführen. Schneller und effizienter geht es aber in einem "richtigen" Kurs, also in einer Schulung mit einem erfahrenen Dozenten. An instance of an old-style class is always implemented from a single built-in type called instance.