Main / Python
Python Version WoesRunning with the system Python and libraries limits you to one specific Python version, chosen by your OS provider. Trying to run all Python applications on one Python installation makes it likely that version conflicts will occur among the collection of libraries. It's also possible that changes to the system Python will break other OS features that depend on it. Nifty tool to analyze what version a Python application needs: https://github.com/ghewgill/pyqver/tree/master Python Usagemain() functionPython scripts require an explicit call to the main function to execute the code within it. Unlike some other programming languages, Python doesn't automatically run a main function. In your .py file, the left-indented code just runs like a script. To execute the main function, you can add the following code block at the end of your script: def main(): print("Hello, world!") if __name__ == "__main__": main() super() functionBasically allows re-direction of method calls, such as accessing inherited methods that have been overridden. For example, if you create a child class but want to use the parent method in addition to extending it with the child method of the same name, you could use super(Child, self).method(). https://docs.python.org/2/library/functions.html?highlight=super#super Module NamesIn Python, a module is often synonymous with a .py file that contains a collection of related classes and functions. Do not use dashes in the names, prefer underscore. The Python suggestion is to use lowercase names only. Switch (Match)From Python 3.10 there is a new match operator, but case ends do not need a break statement as they do not drop to the case below. Also, default is required. Class __init___Use __init__ instead of init for invoking name mangling, which will allow re-definition of the init function for inherited classes. Init in Python is a bit like a constructor. It does not have a return value, so you must use exceptions and tries to catch a problem. Class member variables declared outside the init function are actually static and shared with all instances of the class. For instance variables, declare them inside init. VERY different from C++. pip Installerpip is a Python package installer. Note that the executable path of packages installed via pip are placed in /home/username/.local/bin by default. CommentsPython doesn't truly have a multi-line comment feature, but you can use triple quotes Variable Function ArgsIn Python, *args and **kwargs are used to pass a variable number of arguments to a function. Example def print_all(a, b, *args, c=10, **kwargs): No other arguments should follow **kwargs. Something like a C struct?From Python 3.7 there is a new package that lets you use data classes, which are classes designed to just be data structures. See https://stackoverflow.com/questions/35988/c-like-structures-in-python ListsYou can return a populated list from a function, but it requires use of the append() method. # won't work, creates new local variable def fill_list(mylist): mylist = [0,1,2] #will work def fill_list(mylist): mylist.append([0,1,2]) # or use .extend() function |